2014-10-28 37 views
0

我想以編程方式創建一個視圖層次使用自動佈局的層次結構如下: 的UIView - > 的UIWebView 我想在UIView的底部留下一個UIToolbar一些空間。與自動佈局創建視圖層次編程

結果應在本XIB文件: enter image description here

到目前爲止的代碼:

- (void)loadView 
{ 
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    self.view = view; 

    self.webView = [[UIWebView alloc] init]; 
    self.webView.scalesPageToFit = YES; 
    [self.view addSubview:self.webView]; 

    self.view.translatesAutoresizingMaskIntoConstraints = NO;  
    self.webView.translatesAutoresizingMaskIntoConstraints = NO; 

    NSDictionary *nameMap = @{@"webView" : self.webView}; 

    NSArray *c1Array = 
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[webView]-0-|" 
              options:0 
              metrics:nil 
               views:nameMap]; 

    NSArray *c2Array = 
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[webView]-45-|" 
              options:0 
              metrics:nil 
               views:nameMap]; 

    [self.view addConstraints:c1Array]; 
    [self.view addConstraints:c2Array]; 
} 

當我跟蹤使用:

po [[UIWindow keyWindow] _autolayoutTrace] 

我得到這兩個的UIView和一個UIWebView佈局模糊。

什麼能與我的方法的問題?

回答

1

你不應該設置translatesAutoresizingMaskIntoConstraints爲NO控制器的self.view。如果您刪除該行,您擁有的限制應該足夠。順便說一句,沒有必要在你的格式字符串中的零(儘管它們也不會傷害),

"H:|[webView]|"相當於這個"H:|-0-[webView]-0-|"

+0

謝謝!就是這樣! – Ravul 2014-10-28 18:16:45

相關問題