問題描述
我的 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) 假設您是通過代碼實現的一切,您將需要查找以下屬性 - edgesForExtendedLayout
和 extendedLayoutIncludesOpaqueBars
.已經有 很好的答案 所以我不會在這里覆蓋它.
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以編程方式在導航欄下方設置滾動視圖約束的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!