ios - UINavigationController + 状态栏框架的CGRect

标签 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。

最佳答案

您正在为 UINavigationBar 设置渐变。导航栏的大小是44,如果有状态栏,它的 y 位置是20`。因此,如果您将渐变设置为导航栏,那么肯定只会设置它。它绝对不会影响状态栏,因为它是不同的东西。

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

只需隐藏特定此 View Controller 的默认导航栏,并将大小为 64 的 uiview 拖动到 (0,0) 处,这样它就会覆盖状态栏和导航栏酒吧空间。

然后为该 View 设置渐变,您将获得想要的效果。

希望这会有所帮助:)

关于ios - UINavigationController + 状态栏框架的CGRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37561733/

相关文章:

android - react 原生 : Is it possible to have a Navigator inside a View?

ios - swift 如何使用 Controller 从本地设备中选取文件

ios - 国际象棋游戏的 Sprite Kit 游戏设计

ios - Swift 在两个 View Controller 上使用后退按钮

ios - 永久删除行(或数组项) - swift 3/4

ios - Crashlytics 下载文件

ios - 如何快速在现有类中添加变量?

swift - 尝试在 Pubnub 中创建 channel 时使用未声明的类型 'PNChannel'

iphone - iOS:如何识别我们从父 UIViewController 中的子 UIViewController 返回?

ios - swift 3 : popToViewController not working