問(wèn)題描述
我的 UIViewController 嵌入在導(dǎo)航控制器中.我以編程方式添加導(dǎo)航按鈕,現(xiàn)在嘗試在此導(dǎo)航欄下方添加一個(gè)滾動(dòng)視圖.我遇到的問(wèn)題是,這是填充全幀大小并位于導(dǎo)航欄下方.
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.
如何以編程方式設(shè)置此滾動(dòng)視圖的約束?
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的回答肯定是對(duì)的,但是如果你不需要透明度,想在導(dǎo)航欄下啟動(dòng),真正正確的方法是改變ViewController的行為所以它適合內(nèi)容.為此,您有兩種選擇:
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) 假設(shè)您有 Storyboard,轉(zhuǎn)到 ViewController Attributes Inspector 并禁用Under top bars"
1) Assuming you have Storyboard, go to ViewController Attributes Inspector and disable "Under top bars"
2) 假設(shè)您是通過(guò)代碼實(shí)現(xiàn)的一切,您將需要查找以下屬性 - edgesForExtendedLayout
和 extendedLayoutIncludesOpaqueBars
.已經(jīng)有 很好的答案 所以我不會(huì)在這里覆蓋它.
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.
希望對(duì)你有幫助!
這篇關(guān)于Swift iOS以編程方式在導(dǎo)航欄下方設(shè)置滾動(dòng)視圖約束的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!