使用编程方式创建带有子视图的UIViewController

5
我想创建一个控制器,其中包含一个UIWebView作为子视图的编程方式。我通过以下代码成功实现了它:
VLUpdateViewController.h
@interface VLUpdateViewController : UIViewController

@property (nonatomic, strong) UIWebView* webView;

@end

VLUpdateViewController.m

@implementation VLUpdateViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor blueColor]];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [self.webView setBackgroundColor:[UIColor redColor]];
    [self.webView setScalesPageToFit:YES];
    [self.view addSubview:self.webView];

    NSArray* constraints = [[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[webView]-0-|" options:0 metrics:nil views:@{@"webView" : self.webView}] arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[webView]-0-|" options:0 metrics:nil views:@{@"webView" : self.webView}]];

    [self.view addConstraints:constraints];
}

然而,我的问题是 webView 在方向改变时无法正确地调整大小。事实上,它根本不会调整大小,只是在屏幕中央旋转。
纵向:
横向:
我试图省略`setTranslatesAutoresizingMaskIntoConstraints:`,但当我将方向改为横向时,我得到了以下结果。

当设备方向改变时,您需要更新UIWebview的框架并更新约束。 - Yogesh Suthar
当我在故事板上操作时没有问题,但是那个问题的正确答案对我不起作用。 - halileohalilei
@YogeshSuthar 我该怎么做? - halileohalilei
@halileohalilei 请查看 https://dev59.com/hG855IYBdhLWcg3wg0nC#4301828 - Yogesh Suthar
1
你们为什么把这个标记为重复?你们有看过提出的答案吗?那不是这个问题的答案!这个问题要求一个编程方法。 - justMartin
2个回答

1

我复制并粘贴了您的代码,并添加了缺失的那一行:

self.webView.translatesAutoresizingMaskIntoConstraints = NO;

你已经将 translatesAutoresizingMaskIntoConstraints 设置为 NO,但只对 self.view 起作用,而没有对 self.webView 生效。

最终代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.view setBackgroundColor:[UIColor blueColor]];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [self.webView setBackgroundColor:[UIColor redColor]];
    [self.webView setScalesPageToFit:YES];



    // ---------------------------------------
    // this line is missing
    // ---------------------------------------
    self.webView.translatesAutoresizingMaskIntoConstraints = NO;



    [self.view addSubview:self.webView];

    NSArray* constraints = [[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[webView]-0-|" options:0 metrics:nil views:@{@"webView" : self.webView}] arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[webView]-0-|" options:0 metrics:nil views:@{@"webView" : self.webView}]];

    [self.view addConstraints:constraints];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com/"]];

    [self.webView loadRequest:request];
}

当我这样做时,只会得到一个黑屏,网页无法加载。 - halileohalilei
还有其他干扰因素,那你贴的是真实的代码吗?看起来你正在使用自定义视图控制器,也许你的视图控制器的视图不会立即加载。在这种情况下,请尝试延迟约束设置和网页加载,直到视图出现后再进行。看看是否有帮助。 - Zhang
我正在从另一个UIVIewController创建此控制器,并使用presentViewController:animated:completion:以模态方式呈现它。这可能是问题所在吗? - halileohalilei

-1
使用Autolayout最简单的方法是Masonry。这样做可以避免编写多行代码,从而使工作更加完美。
添加Masonry框架(最好使用pod)并导入类。 然后使用以下代码: //下面这行代码表示webview在任何状态下都应该与superview具有相同的大小
[self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview);
}];

如果您喜欢有一些填充:

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superview.mas_top).with.offset(padding.top); 
        make.left.equalTo(superview.mas_left).with.offset(padding.left);
        make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
        make.right.equalTo(superview.mas_right).with.offset(-padding.right);
    }];

然后,就这么多了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接