gpt4 book ai didi

ios - 将导航栏添加到没有导航 Controller 的 View Controller

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:54 25 4
gpt4 key购买 nike

如何将导航栏添加到未嵌入导航 Controller 的 View Controller (实际上是 Collection View Controller )?我尝试将导航栏拖到 View 上,但它就是不粘。这是在 swift 。

最佳答案

尝试将此代码放入您的 viewDidLoad:

let height: CGFloat = 75
let navbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: height))
navbar.backgroundColor = UIColor.whiteColor()
navbar.delegate = self

let navItem = UINavigationItem()
navItem.title = "Title"
navItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .Plain, target: self, action: nil)
navItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .Plain, target: self, action: nil)

navbar.items = [navItem]

view.addSubview(navbar)

collectionView?.frame = CGRect(x: 0, y: height, width: UIScreen.mainScreen().bounds.width, height: (UIScreen.mainScreen().bounds.height - height))

height 当然可以是任何你想要的。 UIBarButton 的操作是您想要的任何功能的选择器。 (您也根本不需要按钮)。

编辑:

  1. 调整了 collectionView 的框架,使其不会与 UINavigationBar 重叠。
  2. 使高度成为常量,这样它的所有引用都可以在一个地方更改。

关于ios - 将导航栏添加到没有导航 Controller 的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com