ios - 以编程方式创建带有 subview 的 UIViewController

标签 ios objective-c uiwebview

<分区>

我想以编程方式创建一个将 UIWebView 作为 subview 的 Controller 。我设法用下面的代码来做到这一点:

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 没有正确调整大小。事实上,它根本不会调整大小。它只是在屏幕的中心旋转。

肖像:

enter image description here

横向:

enter image description here

我尝试省略 setTranslatesAutoresizingMaskIntoConstraints: 但当我将方向更改为横向时,我得到了以下结果。

enter image description here

最佳答案

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

self.webView.translatesAutoresizingMaskIntoConstraints = NO;

您的 self.view 有 translatesAutoresizingMaskIntoConstraints = NO,但 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];
}

关于ios - 以编程方式创建带有 subview 的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24568977/

相关文章:

ios - 有没有一种通过 iCloud 集成备份 Core Data 数据库的好方法?

ios - 启用分页和每页多个单元格时奇怪的 UICollectionView 行为

ios - 无法获取 UITextField 变量

swift - 在 swift 中加载 UIWebView 中的 URL 时发生 fatal error

iPhone SDK - 嵌入 YouTube 视频的 UIWebView

ios - 如何在 NSAttributedString 中创建可点击链接

ios - iPhone 上的 Http 直播延迟

ios - NSData :writeToFile returning error "File doesn' t exist"

iphone - 在 iPhone 上滚动时重复图像

html - 只获取 HTML 正文,没有标签