user717452
user717452

Reputation: 111

WKWebView Constraints Don't Work

I just want the WKWebView to be pinned to all sides of the self.view, so that it will always be stretched as far as possible no matter rotation. Using the following code, it will fill the view for whatever the initial rotation is, but after rotating, it simply all disappears:

-(void)viewWillAppear:(BOOL)animated {
    [super viewDidLoad];
    self.title = @"Worship Slides";

    self.productURL = @"http://www.316apps.com/Fritch/worship.key";

    NSURL *url = [NSURL URLWithString:self.productURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];

    _theWorship = [[WKWebView alloc] initWithFrame:self.view.frame];
    [_theWorship setTranslatesAutoresizingMaskIntoConstraints:NO];

    [_theWorship loadRequest:request];
    _theWorship.frame = CGRectMake(0, 0, self.navigationController.view.bounds.size.width, self.navigationController.view.bounds.size.height);
    [self.view addSubview:_theWorship];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_theWorship attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
}

Upvotes: 0

Views: 608

Answers (2)

Josh Homann
Josh Homann

Reputation: 16327

Get rid of addConstraint and call isActive = true instead. See the documentation:

When developing for iOS 8.0 or later, set the constraint’s active property to true instead of calling the addConstraint(_:) method directly. The isActive property automatically adds and removes the constraint from the correct view.

Alternatively use NSLayoutAnchor; its not as long winded as NSLayoutConstraint. I only use NSLayoutConstraint in a loop or when I cannot express a constraint with NSLayoutAnchor (ie center multiply).

Upvotes: 1

user5890979
user5890979

Reputation:

-(void)viewWillAppear:(BOOL)animated {
    **[super viewDidLoad];**

Change to:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

Also make sure you add a method to check if your code has already been called, otherwise it will get called multiple times /everytime the view will appear. You should rather call it from viewDidLoad method instead , but you can choose whatever as long as you don't call it multiple times.

Also , after adding everything you can call:

[_theWorship layoutIfNeeded];

Upvotes: 0

Related Questions