gpt4 book ai didi

ios - WKWebView 约束不起作用

转载 作者:行者123 更新时间:2023-11-29 00:30:37 24 4
gpt4 key购买 nike

我只是希望 WKWebView 固定到 self.view 的所有侧面,这样无论旋转它总是尽可能地拉伸(stretch)。使用以下代码,它将填充 View 以适应初始旋转,但旋转后,它会全部消失:

-(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。请参阅文档:

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.

或者使用 NSLayoutAnchor ;它不像 NSLayoutConstraint 那样冗长。我只在循环中或当我无法用 NSLayoutAnchor 表达约束时(即居中乘法)使用 NSLayoutConstraint。

关于ios - WKWebView 约束不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41964845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com