Is it possible to pin a UIView's top to the bottom of the navigation bar?

和自甴很熟 提交于 2019-12-22 10:54:22

问题


I'm trying to position my UIView to be 20pt under the navigation bar, but when I set it relative to the view on the view controller it's still under the navigation bar at 20pt, and I don't want to hardcode it.

Is it possible to position it away from the navigation bar?


回答1:


Try adding contraint with TopLayoutGuide,

The main difference between TopLayoutGuide and self.view is, top layout guide starts with bottom of status bar + bottom of navigation bar(if exist), but self.view always start from (0,0) coordinate in iOS 7 for translucent navigation bar.

So in your case you should try pinning from top layout guide.




回答2:


To do this programmatically use the topLayoutGuide of your view controller:

override func viewDidLoad() {
    ...
    myView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive=true
    ...
}



回答3:


So your UIView sits within a UIViewController, correct?

When you define your initial inner UIView to that UIViewController, can you set its coordinates to be 20px off the top of the ViewController?

For example:

//
//  ViewControllerEx.swift
//

import UIKit

class ViewControllerExample: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        var innerContainer = UIView(frame: CGRectMake(0, 20, self.view.bounds.width, self.view.bounds.height-20))

        self.view.addSubview(innerContainer)
    }  
}


来源:https://stackoverflow.com/questions/26837957/is-it-possible-to-pin-a-uiviews-top-to-the-bottom-of-the-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!