我正在努力在 NavigationBar 后面添加渐变。我将 UINavigationController 子类化来这样做。渐变应填充状态栏和 UINavigationBar。问题是我只能让它填充 UINavigationBar(通过 self.toolBar.bounds)。 我尝试添加状态栏的高度,但它不允许 +
@IBOutlet weak var toolBar: UINavigationBar!
let gradient:CAGradientLayer = CAGradientLayer()
gradient.frame = self.toolBar.bounds
gradient.colors = [UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
self.toolBar.layer.insertSublayer(gradient, atIndex: 1)
toolBar.barTintColor = UIColor.grayColor()
渐变应该延伸到状态栏,所以不会有 grayColor。
最佳答案
您正在为 UINavigationBar 设置渐变。导航栏的大小是
44,如果有状态栏,它的 y 位置是
20`。因此,如果您将渐变设置为导航栏,那么肯定只会设置它。它绝对不会影响状态栏,因为它是不同的东西。
在您的情况下,我认为您应该使用 UIView
制作自定义导航栏。
只需隐藏特定此 View Controller 的默认导航栏,并将大小为 64
的 uiview 拖动到 (0,0)
处,这样它就会覆盖状态栏和导航栏酒吧空间。
然后为该 View 设置渐变,您将获得想要的效果。
希望这会有所帮助:)
关于ios - UINavigationController + 状态栏框架的CGRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37561733/