简体   繁体   English

以编程方式添加自动布局约束将uiview变为黑色

[英]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.

相关问题 将带有自动布局的自定义 UIView 添加到 iCarousel - Adding custom UIView with autolayout to iCarousel 自动布局:在不知道高度限制的情况下隐藏UIView - Autolayout: Hiding a UIView without knowing Height constraint iOS故事板AutoLayout并添加约束 - iOS storyboard AutoLayout and adding constraint 以编程方式从超级视图中删除UIView(自动布局) - Remove UIView from superview programmatically (autolayout) 使用自动布局以编程方式添加背景图像视图 - Adding a background imageview programmatically with autolayout 以编程方式将UINavigationBar添加到UIView? - Adding UINavigationBar to UIView Programmatically? 将UIView的内容渲染为OpenGL ES纹理,但只有黑色 - Render contents of UIView as an OpenGL ES texture but only black turns out 使用自动布局约束时,UIView 不会通过 UIpangesture 平滑拖动 - UIView not dragging smoothly by UIpangesture when use autolayout constraint 以编程方式在两个文本字段之间添加uiview,该文本字段是使用自动布局在xib上创建的 - Add a uiview programmatically between two textfields which was created on xib with autolayout 使用AutoLayout(swift)以编程方式将UILabel添加到ScrollView - Adding UILabel to ScrollView Programmatically with AutoLayout (swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM