Swift iOS以编程方式在导航栏下方设置滚动视图约束

Swift iOS Set scrollView constraint below navigation bar programmatically(Swift iOS以编程方式在导航栏下方设置滚动视图约束)
本文介绍了Swift iOS以编程方式在导航栏下方设置滚动视图约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的 UIViewController 嵌入在导航控制器中.我以编程方式添加导航按钮,现在尝试在此导航栏下方添加一个滚动视图.我遇到的问题是,这是填充全帧大小并位于导航栏下方.

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) 假设您有 Storyboard,转到 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以编程方式在导航栏下方设置滚动视图约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
Resume game cocos2d(恢复游戏 cocos2d)
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)