问题描述
我只想将 WKWebView 固定到 self.view 的所有侧面,这样无论旋转如何,它都会尽可能地拉伸.使用以下代码,无论初始旋转是什么,它都会填充视图,但是在旋转之后,它就完全消失了:
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]];
}
推荐答案
去掉 addConstraint 并调用 isActive = true 代替.请参阅文档:
Get rid of addConstraint and call isActive = true instead. See the documentation:
在为 iOS 8.0 或更高版本开发时,将约束的 active 属性设置为 true而不是直接调用 addConstraint(_:) 方法.isActive 属性会自动从正确的视图中添加和删除约束.
或者使用 NSLayoutAnchor;它不像 NSLayoutConstraint 那样冗长.我只在循环中使用 NSLayoutConstraint 或者当我无法用 NSLayoutAnchor 表达约束时(即中心乘法).
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).
这篇关于WKWebView 约束不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!