2017-09-22 117 views
3

到目前为止我处理梯度导航栏下面的manner_处理导航栏背景渐变

let gradient = CAGradientLayer() 
    let sizeLength = UIScreen.main.bounds.size.height * 2 
    let defaultNavigationBarFrame = CGRect(x: 0, y: 0, width: sizeLength, height: 64) 
    gradient.frame = defaultNavigationBarFrame 
    gradient.colors = [UIColor(hex:"92CF1F").cgColor, UIColor(hex:"79AB1B").cgColor] 

    UINavigationBar.appearance().setBackgroundImage(self.image(fromLayer: gradient), for: .default) 
    UINavigationBar.appearance().tintColor = UIColor.white 
    UINavigationBar.appearance().isTranslucent = false 
    UINavigationBar.appearance().clipsToBounds = false 

    if DeviceType.IS_IPAD{ 
     UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : UIFont .systemFont(ofSize: 24, weight: UIFontWeightLight), NSForegroundColorAttributeName: UIColor.white] 
    } 
    else 
    { 
     UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : UIFont .systemFont(ofSize: 20, weight: UIFontWeightLight), NSForegroundColorAttributeName: UIColor.white] 
    } 

    UISearchBar.appearance().backgroundColor = UIColor.clear 

但现在在iPhone XI有问题,由于“64”作为导航栏高度梯度below_

enter image description here

请建议本可以在每种情况下可以使用动态修复。

回答

0

您不仅可以再假定导航栏高度为64,您不能再假设静态高度。

我认为实现你想要做的最简单的方法是使用UINavigationControllerinit(navigationBarClass:toolbarClass:)方法与自定义UINavigationBar子类覆盖layerClass直接指定CAGradientLayer

不幸的是,我不认为有一种方法可以通过UIAppearance“发射并忘记”。

1

我已经通过屏幕高度检查了iPhone X模式,然后更改生成的渐变图像以填充导航栏。 我认为这不是最好的解决方法,但它现在工作正常。

class func checkiPhoneModel() -> String { 
    if UIDevice().userInterfaceIdiom == .phone { 
     switch UIScreen.main.nativeBounds.height { 
     case 1136: 
      return "5, 5s, 5c, se" 
     case 1334: 
      return "6, 6s, 7" 
     case 2208: 
      return "6+, 6S+, 7+" 
     case 2436: 
      return "X" 
     default: 
      return "unknown" 
     } 
    } 
    return "" 
} 

然后可以创建用于UINavigationBar的扩展,增加了方法来改变颜色的梯度图像:

extension UINavigationBar { 

    func setGradientBackground(colors: [UIColor]) { 

     var size:CGFloat 

     if Reachability.checkiPhoneModel() == "X" { 
      size = 44 
     } 
     else { 
      size = 20 
     } 

     var updatedFrame = bounds 
     updatedFrame.size.height += size 
     let gradientLayer = CAGradientLayer(frame: updatedFrame, colors: colors) 

     setBackgroundImage(gradientLayer.creatGradientImage(), for: UIBarMetrics.default) 
    } 
} 

大小变量被设置为44时是iPhone X,等机型它接收20

然后在您的viewDidLoad,只需调用最近创建的方法:

self.navigationController?.navigationBar.setGradientBackground(colors: [.red, .yellow]) 

The result is something like this. In my case, the gradient uses alpha zero at the bottom of the navigationBar

+0

那么,这是一个很好的实现方式,但现在我只是检查了 static let IS_IPHONE_X = UIDevice.current.userInterfaceIdiom == .phone && max(UIScreen.main.bounds.size.width,UIScreen.main.bounds .size.height)== 812.0 如果DeviceType.IS_IPHONE_X { defaultNavigationBarFrame =的CGRect(X:0,Y:0,宽度:sizeLength,高度:88) } 否则{ defaultNavigationBarFrame =的CGRect(X:0, y:0,width:sizeLength,height:64) } –

0

使用阿利森Barauna的回答你的问题,我设法只是更新我的UINavigationBar的扩展来解决这个问题是这样的:

extension UINavigationBar { 

@objc func setGradientBackground(colors: [UIColor]) { 

    var updatedFrame = bounds 

    if UIDevice().userInterfaceIdiom == .phone { 
     if UIScreen.main.nativeBounds.height == 2436{ 
      updatedFrame.size.height += 44 
     } else { 
      updatedFrame.size.height += 20 
     } 
    } 

    let gradientLayer = CAGradientLayer(frame: updatedFrame, colors: colors) 
    setBackgroundImage(gradientLayer.createGradientImage(), for: UIBarMetrics.default) 
} 

通过该做的高度将只是被设置为大iPhone X大小(44),如果程序检测到正在使用iPhone X(因为屏幕高度为2436)。