objective-c 移除 Navigationbar 的底部边框 iOS7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19047286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:02:05  来源:igfitidea点击:

Remove Navigationbar's bottom border iOS7

objective-cuinavigationbarios7xcode5

提问by Gnamm

Is there a way to delete the bottom border that iOS7 automatically displays under the navigationbar?

有没有办法删除iOS7导航栏下自动显示的底部边框?

回答by wolffan

That does not work on iOS7 with navigation translucent or not...

这在导航半透明或不透明的 iOS7 上不起作用......

A paste from Apple documentation;

来自 Apple 文档的粘贴;

Description The shadow image to be used for the navigation bar. The default value is nil, which corresponds to the default shadow image. When non-nil, this property represents a custom shadow image to show instead of the default. For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

描述 用于导航栏的阴影图像。默认值为 nil,对应默认的阴影图像。当非零时,此属性表示要显示的自定义阴影图像而不是默认值。要显示自定义阴影图像,还必须使用 setBackgroundImage:forBarMetrics: 方法设置自定义背景图像。如果使用默认背景图像,则无论此属性的值如何,都将使用默认阴影图像。

So basically you need to implement that setBackgroundImage. Additional note, on iOS7 you won't use appearance anymore but you'll modify the Navigation bar in the viewController context where you are now.

所以基本上你需要实现那个setBackgroundImage。 附加说明,在 iOS7 上,您将不再使用外观,但您将修改您现在所在的 viewController 上下文中的导航栏。

That is:

那是:

    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];

In my case I put this in viewDidLoad (custom behavior can be added for each UIViewController in the UINavigationViewController).

在我的例子中,我把它放在 viewDidLoad 中(可以为 UINavigationViewController 中的每个 UIViewController 添加自定义行为)。

回答by muffe

If i understand you correctly try

如果我理解你正确尝试

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

回答by user1105951

based on muffed2k answer+ programming Thomas comment, this is what I'm using to show UINavigationBar without background image (ios5.1/6.0) and without bottom border(ios7.0) :

基于muffed2k answer+编程Thomas评论,这就是我用来显示没有背景图像(ios5.1/6.0)和没有底边框(ios7.0)的UINavigationBar的内容:

  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6)
    {
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }else
    {
        [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }

回答by A Fader Darkly

If you're using Swift and you come across this question, try this in your main ViewController:

如果您正在使用 Swift 并且遇到这个问题,请在您的主 ViewController 中试试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    /// ...

    navigationController?.navigationBar.shadowImage = UIImage();
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)

    //...
}

Based on @wolffan's answer above

基于@wolffan上面的回答

回答by Matthieu Riegler

For me follwing worked on iOS 7 to 9+ when translucentis set to false

对我来说,当translucent设置为时,以下适用于 iOS 7 到 9+false

UINavigationBar.appearance().transluscent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics:.Default)

回答by Matthieu Riegler

I know there is an accepted answer to this already but another way to do it is setting clipToBounds to true.

我知道这已经有一个公认的答案,但另一种方法是将 clipToBounds 设置为 true。

Here is the one line of code to do it in swift

这是快速完成的一行代码

self.navigationController?.navigationBar.clipsToBounds = true

Worked for me like a charm.

像魅力一样对我来说有效。

回答by Aftab

for objective c

对于目标 c

self.navigationController.navigationBar.clipsToBounds = YES;

回答by Fabrizio Prosperi

Works like a charm: Swift 3.x version

魅力四射:Swift 3.x 版本

    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

回答by kraftydevil

If you are targeting iOS 7 and are notsetting a background image then this will work:

如果您的目标是 iOS 7 并且没有设置背景图像,那么这将起作用:

        CGFloat navigationBarWidth = self.navigationController.navigationBar.frame.size.width;
        CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
        CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(navigationBarWidth, navigationBarHeight + statusBarHeight), NO, 0.0);
        UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];

        //the following line takes away the border but only works if a background image is set (above)
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

I got the idea from @muffe2k's answer and this SO post.

我从@muffe2k 的回答和这篇 SO post 中得到了这个想法。