我正在尝试以编程方式向UIWebView
添加UIViewController
,但我收到错误。我在下面粘贴了代码和错误(代码在viewDidLoad
:
[self.view addSubview:self.webView];
NSDictionary *views = @{@"webView": self.webView};
NSArray *horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[webView]|"
options:0
metrics:nil
views:views];
NSArray *vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[webView]|"
options:0
metrics:nil
views:views];
NSMutableArray *allConstraints = [NSMutableArray new];
[allConstraints addObjectsFromArray:horizontal];
[allConstraints addObjectsFromArray:vertical];
[self.view addConstraints:allConstraints];
- (UIWebView *)webView {
if (!_webView) {
_webView = [[UIWebView alloc] init];
_webView.scalesPageToFit = YES;
_webView.delegate = self;
}
return _webView;
}
我得到的错误是:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x14dbc5e0 V:|-(0)-[UIWebView:0x14da8010] (Names: '|':UIView:0x14dae350 )>",
"<NSLayoutConstraint:0x14dbc640 V:[UIWebView:0x14da8010]-(0)-| (Names: '|':UIView:0x14dae350 )>",
"<NSAutoresizingMaskLayoutConstraint:0x14dc2880 h=--& v=--& UIWebView:0x14da8010.midY == + 284>",
"<NSAutoresizingMaskLayoutConstraint:0x14e93e60 h=-&- v=-&- UIView:0x14dae350.height == _UIParallaxDimmingView:0x14dbcca0.height>",
"<NSAutoresizingMaskLayoutConstraint:0x14ec1130 h=--& v=--& V:[_UIParallaxDimmingView:0x14dbcca0(504)]>")
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x14dbc640 V:[UIWebView:0x14da8010]-(0)-| (Names: '|':UIView:0x14dae350 )>
Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
我正在尝试将UIWebView
设置为设备的宽度,并从顶部的导航栏下方开始设置它的高度。这里还有什么我想念的吗?我为此阅读了以下内容:
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
http://derpturkey.com/autosize-uitableviewcell-height-programmatically/
答案 0 :(得分:2)
通常NSAutoresizingMaskLayoutConstraint
的存在表示您需要为一个或多个视图将translatesAutoresizingMaskIntoConstraints
设置为NO。试试这个:
[self.view addSubview:self.webView]; // you already have this line.
self.webview.translatesAutoresizingMaskIntoConstraints = NO; //add this line.
您也可以尝试使用webView
的getter方法设置属性,而不是将其放在viewDidLoad
中。
另外,您是否在覆盖[super viewDidLoad]
?
viewDidLoad