Reputation: 111
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
Reputation: 16327
Get rid of addConstraint and call isActive = true instead. See the documentation:
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
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