ios - 如何将 UIView 固定在 iOS 11 NavigationBar 不断变化的高度框架下方?

标签 ios iphone swift ios11 mobile-development

我有一个导航栏和一个 Collection View 以编程方式添加到我的应用程序中,我也在尝试添加一个自定义 View 。我已经让 View 位于 Collection View 上方和导航栏下方,这是我想要的,但是,在 iOS 11 上,导航栏的高度会发生变化,具体取决于您是否向下拉伸(stretch) Collection View 。我也想让 View 向下移动,以便 Collection View 和导航栏之间没有空白,因为我的自定义 View 不会进一步向下移动,因为 View 的 safeInsets 不会更改。它将向上移动到折叠标题,这不是问题。

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Home"

    navigationController?.navigationBar.prefersLargeTitles = true

    collectionView?.backgroundColor = UIColor.white
    collectionView?.translatesAutoresizingMaskIntoConstraints = false
    //collectionView?.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
    //collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)

    collectionView?.register(StockCell.self, forCellWithReuseIdentifier: "cellid")

    setupMenuBar()
}

let menuBar: MenuBar = {
    let mb = MenuBar()
    mb.translatesAutoresizingMaskIntoConstraints = false
    mb.backgroundColor = .black
    return mb
}()

private func setupMenuBar() {
    view.addSubview(menuBar)
    NSLayoutConstraint.activate([
        (collectionView?.topAnchor.constraint(equalTo: menuBar.bottomAnchor))!,
        (collectionView?.widthAnchor.constraint(equalTo: view.widthAnchor))!,
        (collectionView?.bottomAnchor.constraint(equalTo: view.bottomAnchor))!,
        menuBar.heightAnchor.constraint(equalToConstant: 50),
        menuBar.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
        menuBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
    ])

}

This is the starting screen, showing the view in its resting position

This is the navigation bar overlapping the view, which is what I want to fix

最佳答案

您可以将自定义菜单 View 的底部 anchor 约束到导航栏的底部 anchor ;这样,菜单 View 将始终保持相同的垂直偏移(不过您可能必须熟悉其他一些约束):

navigationController!.navigationBar.bottomAnchor.constraint(equalTo: menuBar.bottomAnchor)

参见 this answer一个扩展的例子。

关于ios - 如何将 UIView 固定在 iOS 11 NavigationBar 不断变化的高度框架下方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48718086/

相关文章:

ios - 'tm' 的定义必须在需要之前从模块 'Darwin.C.time' 导入

ios - 如何在 SKView 之上添加 SCNView

ios - 如何在设置中添加定位服务的应用说明

iphone - 如何部署 iPhone 应用程序(无需 App Store)?

ios - 为什么我无法在 Xcode 的连接检查器中删除 UITableViewController 的 'view' 导出?

swift - 在扩展的 CollectionViewCell 旁边为蒙版制作动画

swift - 自定义委托(delegate)方法正在调用

ios - 获取 ImageView 图片原始大小

ios 7 drawViewHierarchyInRect :afterScreenUpdates: black screen on iPad only

iphone - 是否不需要为IOS 4.0多任务支持方案保存状态?