我有一个应用程序,有时需要它的导航栏融入内容。
有人知道怎么去掉或者改变这个烦人的小条的颜色吗?
在下图中,我说的是根视图控制器下面1px的高度线
我有一个应用程序,有时需要它的导航栏融入内容。
有人知道怎么去掉或者改变这个烦人的小条的颜色吗?
在下图中,我说的是根视图控制器下面1px的高度线
当前回答
适用于iOS 13+
窍门是用透明背景初始化'UINavigationBarAppearance'。然后,您可以轻松地删除导航栏的水平线。
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = .green // Required background color
最后,按照apple的建议将外观更改添加到导航项中。
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance
其他回答
我知道这是一个老话题,但我找到了一个非常有效的解决方案:
子类UINavigationBar。 在你的UINavigationBar子类中,用下面的代码重写didAddSubview:
- (void)didAddSubview:(UIView *)subview
{
[super didAddSubview:subview];
if ([subview isKindOfClass:[UIImageView class]]) {
[subview setClipsToBounds:YES];
}
}
试试这个:
[[UINavigationBar appearance] setBackgroundImage: [UIImage new]
forBarMetrics: UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage new];
下图有解释(iOS7导航栏):
检查这个SO问题: iOS7 -改变UINavigationBar的边框颜色
这里有一个不使用任何图像的方法,这是唯一适合我的方法:
self.navigationController.navigationBar.layer.shadowOpacity = 0;
不幸的是,您需要在每个不希望出现该行的文件上执行此操作。在appDelegate中没有办法这样做。
编辑:
设置shadowColor为nil是不需要的,这是你唯一需要的行。
这里有一个非常重要的注意事项——改变UIViewController的navigationItem的外观比直接改变navigationBar要灵活得多。
为什么这么问?
For the simple reason that the navigationItem is tied to a single UIViewController and represents the state of the navigationBar for that particular UIViewController. This is big, as you don't have to handle the navigation bar changes between different view controllers within viewWillAppear (or something similar), as you would if you mutated the navigationBar; which is, remember, shared between all view controllers of a given navigation stack (UINavigationController), and changing it in one place changes it for all view controllers up to the stack.
你只要为一个特定的视图控制器设置正确的UINavigationBarAppearance, UIKit就会正确地更新导航栏样式,这取决于当前哪个视图控制器是导航堆栈上的顶视图控制器。
navigationItem.standardAppearance` = `UINavigationBarAppearance()
下面是破解方法。因为它在关键路径上工作,将来可能会崩溃。但就目前而言,它的运作符合预期。
迅速:
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
Objective - C:
[self.navigationController.navigationBar setValue:@(YES) forKeyPath:@"hidesShadow"];