Swift iOS以编程方式在导航栏下方设置scrollView约束 [英] Swift iOS Set scrollView constraint below navigation bar programmatically

查看:138
本文介绍了Swift iOS以编程方式在导航栏下方设置scrollView约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UIViewController嵌入在导航控制器中.我以编程方式添加了导航按钮,现在尝试在此导航栏下方添加一个scrollView.我遇到的问题是,这是填满整个帧的大小并进入导航栏的下方.

My UIViewController is embedded in a navigation controller. I programmatically add the navigation buttons and now trying to add a scrollView below this navigation bar. The problem I'm having is this is filling the full frame size and going under the navigation bar.

如何以编程方式设置此滚动视图的约束?

How do I programmatically set constraints of this scrollview?

var scrollView: UIScrollView!
var containerView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Filters"
    // add some buttons on the navigation

    self.scrollView = UIScrollView()
    self.scrollView.backgroundColor = UIColor.grayColor()
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)

    containerView = UIView()

    scrollView.addSubview(containerView)
    view.addSubview(scrollView)

    let label = UILabel(frame: CGRectMake(0, 0, 100, 21))
    label.text = "my label"
    containerView.addSubview(label)
}

推荐答案

虽然Clafou的答案肯定是正确的,但是如果您不需要透明度并且想在导航栏下开始,那么真正正确的方法是更改​​ViewController的行为因此它适合内容.为此,您有两个选择:

While Clafou's answer is certainly correct, if you don't need transparency and want to start under navigation bar, the really proper way is to change behavior of the ViewController so it fits the content properly. To do that, you have two options:

1)假设您拥有情节提要,请转到ViewController Attributes Inspector并禁用"Under top bars"

1) Assuming you have Storyboard, go to ViewController Attributes Inspector and disable "Under top bars"

2)假设您已经通过代码完成了所有工作,那么您将需要查找以下属性-edgesForExtendedLayoutextendedLayoutIncludesOpaqueBars.目前已经有最佳答案,所以我不会在这里盖住.

2) Assuming you are everything through code, you will want to look for following properties - edgesForExtendedLayout, and extendedLayoutIncludesOpaqueBars. There is great answer for that already on SO so I won't cover it here.

希望有帮助!

这篇关于Swift iOS以编程方式在导航栏下方设置scrollView约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆