【发布时间】:2013-10-03 13:06:06
【问题描述】:
有没有办法删除iOS7在导航栏下自动显示的底部边框?
【问题讨论】:
标签: objective-c uinavigationbar ios7 xcode5
有没有办法删除iOS7在导航栏下自动显示的底部边框?
【问题讨论】:
标签: objective-c uinavigationbar ios7 xcode5
这不适用于 iOS7 导航是否半透明...
来自 Apple 文档的粘贴;
说明 用于导航栏的阴影图像。 默认值为 nil,对应于默认的阴影图像。当非零时,此属性表示要显示的自定义阴影图像而不是默认值。要显示自定义阴影图像,还必须使用 setBackgroundImage:forBarMetrics: 方法设置自定义背景图像。如果使用默认背景图片,则无论该属性的值如何,都将使用默认阴影图片。
所以基本上你需要实现那个 setBackgroundImage。 补充说明,在 iOS7 上,您将不再使用外观,但您将在您现在所在的 viewController 上下文中修改导航栏。
即:
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
在我的例子中,我把它放在 viewDidLoad 中(可以为 UINavigationViewController 中的每个 UIViewController 添加自定义行为)。
【讨论】:
shadowImage 和 backgroundShadowImage
如果我理解正确,请尝试
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
【讨论】:
基于 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];
}
【讨论】:
如果您使用 Swift 并遇到此问题,请在您的主 ViewController 中尝试:
override func viewDidLoad() {
super.viewDidLoad()
/// ...
navigationController?.navigationBar.shadowImage = UIImage();
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
//...
}
根据上面@wolffan 的回答
【讨论】:
当translucent 设置为false 时,我在iOS 7 到9+ 上工作过
UINavigationBar.appearance().transluscent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics:.Default)
【讨论】:
我知道已经有一个公认的答案,但另一种方法是将 clipToBounds 设置为 true。
这是用 swift 完成的一行代码
self.navigationController?.navigationBar.clipsToBounds = true
像魅力一样为我工作。
【讨论】:
为了目标c
self.navigationController.navigationBar.clipsToBounds = YES;
【讨论】:
像魅力一样工作:Swift 3.x 版本
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
【讨论】:
如果您的目标是 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]];
我从@muffe2k 的回答和this SO post 中得到了这个想法。
【讨论】: