CGRect for UINavigationController + Status Bar的框架

时间:2016-06-01 07:09:49

标签: ios swift uinavigationcontroller uinavigationbar cagradientlayer

我正在为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()

以下是上述代码的截图: Gradient is filling the wrong frame.

渐变应该延伸到状态栏,因此不会有grayColor。

2 个答案:

答案 0 :(得分:2)

如果状态栏在那里,您将渐变设置为UINavigationBar. Navigation bar's size is 44 and it's y position is 20`。因此,如果您将渐变设置为导航栏,那么它肯定只设置在它上面。它绝对不会影响状态栏,因为它是不同的东西。

在您的情况下,我应该使用UIView来制作自定义导航栏。

只需隐藏特定此视图控制器的默认导航栏,然后拖动大小为64的uiview,并将其放在(0,0),这样就会覆盖状态栏和导航栏空间。

并将渐变设置为该视图,您将获得所需的效果。

希望这会有所帮助:)

答案 1 :(得分:-1)

您也可以为渐变提供图像:

[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"gradiant_bg"]]];