迄今为止,我按以下方式处理导航栏中的渐变_
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 X上,由于“64”作为渐变的导航栏高度,我遇到了问题,如下图所示_
请建议一种可以在每种情况下动态使用的解决方法。
updatedFrame.size.height += navigationBar.frame.maxY
,这样你就不必检查设备了。 - Paolo Musolino