[英]Adding autolayout constraint programmatically turns uiview to black
I need a view controller without xib. 我需要一个没有xib的视图控制器。 There should be a webview with fully filled to the view.
应该有一个完全填充视图的Web视图。 For that I've added the following code to loadView method.
为此,我将以下代码添加到loadView方法。
- (void)loadView {
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:applicationFrame];
view.translatesAutoresizingMaskIntoConstraints = NO;
[view setBackgroundColor:[UIColor greenColor]];
[self setView:view];
// //create webview
self.webview = [[UIWebView alloc] initWithFrame:CGRectZero];
self.webview.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:self.webview];
[self.webview setBackgroundColor:[UIColor orangeColor]];
[self.webview setDelegate:self];
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,_webview);
// //add constraints
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_webview]|" options:0 metrics:nil views:viewBindings]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_webview]|" options:0 metrics:nil views:viewBindings]];
}
But this turns the entire view to black. 但这会使整个视图变成黑色。 If I coment
[view addConstraints:...
method calls its showing a green view. 如果我注释
[view addConstraints:...
方法,则调用它显示绿色视图。 What's wrong with my code? 我的代码有什么问题?
I believe that the problem is that the parent view should not set translatesAutoresizingMaskIntoConstraints
to false. 我认为问题在于父视图不应将
translatesAutoresizingMaskIntoConstraints
设置为false。 The only view that must set that attribute to false is the one you are applying autolayout to, in this case webView. 必须将该属性设置为false的唯一视图是您要对其应用自动布局的视图,在本例中为webView。 If you set
view.translatesAutoresizingMaskIntoConstraints
to false then you have to add constraints to view
. 如果将
view.translatesAutoresizingMaskIntoConstraints
设置为false,则必须为view
添加约束。
You don't need to change the root view of the UIViewController
manually, maybe that's why it does not work. 您不需要手动更改
UIViewController
的根视图,也许这就是为什么它不起作用的原因。
My suggestion would be to try this out: 我的建议是尝试一下:
- (void) viewDidLoad {
[super viewDidLoad];
self.webview = [[UIWebView alloc] init];
self.webview.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:self.webview];
[self.webview setBackgroundColor:[UIColor orangeColor]];
[self.webview setDelegate:self];
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(view,_webview);
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_webview]-0-|" options:0 metrics:nil views:viewBindings]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_webview]-0-|" options:0 metrics:nil views:viewBindings]];
// put a breakpoint after this line to see the frame of your UIWebView.
// It should be the same as the view
[self.view layoutIfNeeded];
}
This should work and your UIWebView
should be full screen. 这应该可以工作,并且您的
UIWebView
应该为全屏显示。 Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.