我有一个应用程序,有时需要其导航栏与内容融合在一起。
有谁知道如何摆脱或改变这个烦人的小酒吧的颜色?
在下面的图片中,我遇到了-我正在谈论“ Root View Controller”下方的1px高度线
我有一个应用程序,有时需要其导航栏与内容融合在一起。
有谁知道如何摆脱或改变这个烦人的小酒吧的颜色?
在下面的图片中,我遇到了-我正在谈论“ Root View Controller”下方的1px高度线
Answers:
使用.shadowColor
物业
如果此属性为nil或包含纯色,则该条不显示阴影
例如:
let navigationBar = navigationController?.navigationBar
let navigationBarAppearence = UINavigationBarAppearance()
navigationBarAppearence.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearence
为此,您应该设置一个自定义阴影图像。但是要显示阴影图像,您还需要设置自定义背景图像,请引用Apple文档中的内容:
为了显示自定义阴影图像,还必须使用setBackgroundImage(_:for :)方法设置自定义背景图像。如果使用默认的背景图像,则无论此属性的值如何,都将使用默认的阴影图像。
所以:
let navigationBar = navigationController!.navigationBar
navigationBar.setBackgroundImage(#imageLiteral(resourceName: "BarBackground"),
for: .default)
navigationBar.shadowImage = UIImage()
上面是隐藏它的唯一“官方”方法。不幸的是,它消除了bar的半透明性。
您有以下选择:
纯色,无半透明:
navigationBar.barTintColor = UIColor.redColor()
navigationBar.isTranslucent = false
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()
创建充满色彩的小背景图像并使用它。
使用下面描述的“ hacky”方法。它还将使栏保持半透明。
为了保持透明度,您需要另一种方法,它看起来像hack,但效果很好。我们要消除的阴影是UIImageView
下方的发际线UINavigationBar
。我们可以找到它,并在需要时隐藏/显示它。
以下说明假定您仅需要将发际线隐藏在UINavigationController
层次结构的一个控制器中。
声明实例变量:
private var shadowImageView: UIImageView?
添加找到此阴影的方法(细线) UIImageView:
private func findShadowImage(under view: UIView) -> UIImageView? {
if view is UIImageView && view.bounds.size.height <= 1 {
return (view as! UIImageView)
}
for subview in view.subviews {
if let imageView = findShadowImage(under: subview) {
return imageView
}
}
return nil
}
添加/编辑viewWillAppear/viewWillDisappear
方法:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if shadowImageView == nil {
shadowImageView = findShadowImage(under: navigationController!.navigationBar)
}
shadowImageView?.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
shadowImageView?.isHidden = false
}
相同的方法也适用于UISearchBar
发际线,以及(几乎)您需要隐藏的其他所有东西:)
非常感谢@Leo Natan的初衷!
UINavigationBar
属性的视图:clipsToBounds = YES
clipsToBounds = YES
就像一个魅力!谢谢!
viewWillAppear
被调用都无法得到shadowImageView
下面可以帮助您!
迅速:
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
目标C:
[self.navigationController.navigationBar setValue:@(YES) forKeyPath:@"hidesShadow"];
如果您只想使用纯色的导航栏颜色并将其设置在情节提要中,请在AppDelegate
课堂上使用以下代码通过外观代理删除1像素边框:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
尝试这个:
[[UINavigationBar appearance] setBackgroundImage: [UIImage new]
forBarMetrics: UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage new];
下图有说明(iOS7 NavigationBar):
并检查以下问题: iOS7-更改UINavigationBar边框颜色
想要添加Serhii答案的Swift版本。我UIBarExtension.swift
使用以下内容创建了一个:
import Foundation
import UIKit
extension UINavigationBar {
func hideBottomHairline() {
self.hairlineImageView?.isHidden = true
}
func showBottomHairline() {
self.hairlineImageView?.isHidden = false
}
}
extension UIToolbar {
func hideBottomHairline() {
self.hairlineImageView?.isHidden = true
}
func showBottomHairline() {
self.hairlineImageView?.isHidden = false
}
}
extension UIView {
fileprivate var hairlineImageView: UIImageView? {
return hairlineImageView(in: self)
}
fileprivate func hairlineImageView(in view: UIView) -> UIImageView? {
if let imageView = view as? UIImageView, imageView.bounds.height <= 1.0 {
return imageView
}
for subview in view.subviews {
if let imageView = self.hairlineImageView(in: subview) { return imageView }
}
return nil
}
}
快速的方法:
UINavigationBar.appearance().setBackgroundImage(
UIImage(),
forBarPosition: .Any,
barMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()
快速简单的解决方案
let navigationBar = self.navigationController?.navigationBar
navigationBar?.setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
navigationBar?.shadowImage = UIImage()
在研究了Serhil的答案之后,我创建了一个pod UINavigationBar + Addition,可以轻松隐藏发际线。
#import "UINavigationBar+Addition.h"
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar hideBottomHairline];
}
从iOS 13开始,有一个系统API可以设置或删除阴影
UIKit使用shadowImage和shadowColor属性确定阴影的外观。当shadowImage为nil时,该条显示根据shadowColor属性中的值着色的默认阴影。如果shadowColor为nil或包含clearColor颜色,则该条不显示阴影。
let appearance = UINavigationBarAppearance()
appearance.shadowImage = nil
appearance.shadowColor = nil
navigationController.navigationBar.standardAppearance = appearance
https://developer.apple.com/documentation/uikit/uibarappearance/3198009-shadowimage
UINavigationBarAppearance
!我永远找不到。谢谢,这100%解决了我的问题
pxpgraphics解决方案已针对Swift 2.0更新
extension UINavigationBar {
func hideBottomHairline()
{
hairlineImageViewInNavigationBar(self)?.hidden = true
}
func showBottomHairline()
{
hairlineImageViewInNavigationBar(self)?.hidden = false
}
private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView?
{
if let imageView = view as? UIImageView where imageView.bounds.height <= 1
{
return imageView
}
for subview: UIView in view.subviews
{
if let imageView = hairlineImageViewInNavigationBar(subview)
{
return imageView
}
}
return nil
}
}
extension UIToolbar
{
func hideHairline()
{
let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = true
}
func showHairline()
{
let navigationBarImageView = hairlineImageViewInToolbar(self)?.hidden = false
}
private func hairlineImageViewInToolbar(view: UIView) -> UIImageView?
{
if let imageView = view as? UIImageView where imageView.bounds.height <= 1
{
return imageView
}
for subview: UIView in view.subviews
{
if let imageView = hairlineImageViewInToolbar(subview)
{
return imageView
}
}
return nil
}
}
UINavigationController().navigationBar/toolbar.hide/showBottomHairline()
然后不?我正在尝试此操作,但无济于事。我的理解不正确吗?
Swift 4 //用于隐藏导航栏阴影线
navigationController?.navigationBar.shadowImage = UIImage()
我使用UINavigationBar扩展名,该扩展名使我能够使用UIAppearance API来隐藏/显示阴影,或者选择要使用Storyboard(或源代码)隐藏/显示该阴影的导航栏。这是扩展名:
import UIKit
private var flatAssociatedObjectKey: UInt8 = 0
/*
An extension that adds a "flat" field to UINavigationBar. This flag, when
enabled, removes the shadow under the navigation bar.
*/
@IBDesignable extension UINavigationBar {
@IBInspectable var flat: Bool {
get {
guard let obj = objc_getAssociatedObject(self, &flatAssociatedObjectKey) as? NSNumber else {
return false
}
return obj.boolValue;
}
set {
if (newValue) {
let void = UIImage()
setBackgroundImage(void, forBarPosition: .Any, barMetrics: .Default)
shadowImage = void
} else {
setBackgroundImage(nil, forBarPosition: .Any, barMetrics: .Default)
shadowImage = nil
}
objc_setAssociatedObject(self, &flatAssociatedObjectKey, NSNumber(bool: newValue),
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
现在,要禁用所有导航栏上的阴影,必须使用:
UINavigationBar.appearance().flat = true
或者,您可以使用情节提要板启用/禁用此行为:
flatAssociatedObjectKey
将其视为标识您关联对象的唯一int(视为指针)。这是由私有变量的内存地址定义的。我更新了响应以添加此变量。有关更多信息,请参见nshipster.com/associated-objects。
translucent
为false
Swift 4经过测试的 一线解决方案
在Viewdidload()
设置导航控制器的用户默认值true中,键“ hidesShadow”为true
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
}
斯威夫特把这个
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()
在
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
如果您想保留半透明性并且不想UINavigationController
在应用程序中每个子类都子类化,则可以选择以下另一种选择:
#import <objc/runtime.h>
@implementation UINavigationController (NoShadow)
+ (void)load {
Method original = class_getInstanceMethod(self, @selector(viewWillAppear:));
Method swizzled = class_getInstanceMethod(self, @selector(swizzled_viewWillAppear:));
method_exchangeImplementations(original, swizzled);
}
+ (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
- (void)swizzled_viewWillAppear:(BOOL)animated {
UIImageView *shadow = [UINavigationController findHairlineImageViewUnder:self.navigationBar];
shadow.hidden = YES;
[self swizzled_viewWillAppear:animated];
}
@end
Slightly Swift Solution
func setGlobalAppearanceCharacteristics () {
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.white
navigationBarAppearace.barTintColor = UIColor.blue
navigationBarAppearace.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationBarAppearace.shadowImage = UIImage()
}
在iOS8上,如果你设置UINavigationBar.barStyle
到.Black
你可以设置栏的背景为素色无边界。
在Swift中:
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barStyle = UIBarStyle.Black
UINavigationBar.appearance().barTintColor = UIColor.redColor()
适用于我的两行解决方案。尝试在ViewDidLoad方法中添加它:
navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
self.extendedLayoutIncludesOpaqueBars = true
创建扩展:
extension UIImage {
class func hideNavBarLine(color: UIColor) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let navBarLine = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return navBarLine
}
}
将此添加到viewDidLoad()
:
self.navigationController?.navigationBar.shadowImage = UIImage.hideNavBarLine(color: UIColor.clear)
对于iOS 9用户,这对我有用。只需添加:
UINavigationBar.appearance().shadowImage = UIImage()
您应该在UISearchBar的底部添加一个视图
let rect = searchController.searchBar.frame;
let lineView : UIView = UIView.init(frame: CGRect.init(x: 0, y: rect.size.height-1, width: rect.size.width, height: 1))
lineView.backgroundColor = UIColor.init(hexString: "8CC73E")
searchController.searchBar.addSubview(lineView)
我刚刚为此创建了一个扩展...很抱歉格式化(这是我的第一个答案)。
用法:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.hideShadow = true
}
延期:
UINavigationController.swift
// Created by Ricardo López Rey on 16/7/15.
import Foundation
struct UINavigationControllerExtension {
static var hideShadowKey : String = "HideShadow"
static let backColor = UIColor(red: 247/255, green: 247/255, blue: 248/255, alpha: 1.0)
}
extension UINavigationController {
var hideShadow : Bool {
get {
if let ret = objc_getAssociatedObject(self, &UINavigationControllerExtension.hideShadowKey) as? Bool {
return ret
} else {
return false
}
}
set {
objc_setAssociatedObject(self,&UINavigationControllerExtension.hideShadowKey,newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
if newValue {
self.navigationBar.setBackgroundImage(solidImage(UINavigationControllerExtension.backColor), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = solidImage(UIColor.clearColor())
} else {
self.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
}
}
}
private func solidImage(color: UIColor, size: CGSize = CGSize(width: 1,height: 1)) -> UIImage {
var rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
在AppDelegate中,这已全局更改了NavBar的格式:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = UIColor.redColor()
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = UIColor.redColor()
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
尚未在特定的VC上实现任何其他操作,但这将帮助90%的人
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIImage *emptyImage = [UIImage new];
self.navigationController.navigationBar.shadowImage = emptyImage;
[self.navigationController.navigationBar setBackgroundImage:emptyImage forBarMetrics:UIBarMetricsDefault];
}