在iOS 7中,尝试添加一些约束以确保在更改Web视图的框架后,Web视图处于其superview的这个位置(即以下代码中的Web视图容器)。另外,我想确保Web视图中的滚动视图与Web视图匹配相同的边界,这样我就可以拥有正确的垂直滚动指示器。 Web视图容器的超级视图框架(应用程序窗口的根视图控制器视图)是伪代码(origin.x = 0, origin.y = 20, size.width = 320, size.height = 568 - 20 = 548)
。
代码由我编写,用于添加上述约束。
运行代码时收到的错误消息。不明白为什么我不能添加新约束来替换旧约束。问题是我自己没有创建旧约束,它是在我不知道的地方自动生成的。不要理解错误消息,例如这意味着“NSAutoresizingMaskLayoutConstraint:0xc17e930 h=--& v=--& UIWebView:0xc06b020.midY == + 274
”。
[[_webViewContainer webView] addConstraint:
[NSLayoutConstraint constraintWithItem:[[_webViewContainer webView] scrollView]
attribute:NSLayoutAttributeTop
relatedBy:0
toItem:[_webViewContainer webView]
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
[[_webViewContainer webView] addConstraint:
[NSLayoutConstraint constraintWithItem:[[_webViewContainer webView] scrollView]
attribute:NSLayoutAttributeBottom
relatedBy:0
toItem:[_webViewContainer webView]
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
[[_webViewContainer webView] addConstraint:
[NSLayoutConstraint constraintWithItem:[[_webViewContainer webView] scrollView]
attribute:NSLayoutAttributeLeft
relatedBy:0
toItem:[_webViewContainer webView]
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0]];
[[_webViewContainer webView] addConstraint:
[NSLayoutConstraint constraintWithItem:[[_webViewContainer webView] scrollView]
attribute:NSLayoutAttributeRight
relatedBy:0
toItem:[_webViewContainer webView]
attribute:NSLayoutAttributeRight
multiplier:1
constant:0]];
[[_webViewContainer webView] setNeedsUpdateConstraints];
[[_webViewContainer webView] layoutSubviews];
[_webViewContainer addConstraint:
[NSLayoutConstraint constraintWithItem:[_webViewContainer webView]
attribute:NSLayoutAttributeTop
relatedBy:0
toItem:_webViewContainer
attribute:NSLayoutAttributeTop
multiplier:1
constant:45]];
[_webViewContainer addConstraint:
[NSLayoutConstraint constraintWithItem:[_webViewContainer webView]
attribute:NSLayoutAttributeBottom
relatedBy:0
toItem:_webViewContainer
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-49]];
[_webViewContainer addConstraint:
[NSLayoutConstraint constraintWithItem:[_webViewContainer webView]
attribute:NSLayoutAttributeLeft
relatedBy:0
toItem:_webViewContainer
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0]];
[_webViewContainer addConstraint:
[NSLayoutConstraint constraintWithItem:[_webViewContainer webView]
attribute:NSLayoutAttributeRight
relatedBy:0
toItem:_webViewContainer
attribute:NSLayoutAttributeRight
multiplier:1
constant:0]];
[_webViewContainer setNeedsUpdateConstraints];
[_webViewContainer layoutSubviews];
2013-11-05 21:32:03.422 emptyproject[16471:70b] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0xc17e930 h=--& v=--& UIWebView:0xc06b020.midY == + 274>",
"<NSAutoresizingMaskLayoutConstraint:0xc17e960 h=--& v=--& V:[UIWebView:0xc06b020(454)]>",
"<NSLayoutConstraint:0xc1803f0 V:|-(45)-[UIWebView:0xc06b020] (Names: '|':UIViewWebViewContainer:0xbfb76d0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xc1803f0 V:|-(45)-[UIWebView:0xc06b020] (Names: '|':UIViewWebViewContainer:0xbfb76d0 )>
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.
2013-11-05 21:32:03.423 emptyproject[16471:70b] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0xc17e930 h=--& v=--& UIWebView:0xc06b020.midY == + 274>",
"<NSAutoresizingMaskLayoutConstraint:0xc17e960 h=--& v=--& V:[UIWebView:0xc06b020(454)]>",
"<NSAutoresizingMaskLayoutConstraint:0xc17ed80 h=--& v=--& V:[UIViewWebViewContainer:0xbfb76d0(548)]>",
"<NSLayoutConstraint:0xc1802b0 UIWebView:0xc06b020.bottom == UIViewWebViewContainer:0xbfb76d0.bottom - 49>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xc1802b0 UIWebView:0xc06b020.bottom == UIViewWebViewContainer:0xbfb76d0.bottom - 49>
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.
2013-11-05 21:32:03.424 emptyproject[16471:70b] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0xc17e930 h=--& v=--& UIWebView:0xc06b020.midY == + 274>",
"<NSAutoresizingMaskLayoutConstraint:0xc17e960 h=--& v=--& V:[UIWebView:0xc06b020(548)]>",
"<NSLayoutConstraint:0xc1803f0 V:|-(45)-[UIWebView:0xc06b020] (Names: '|':UIViewWebViewContainer:0xbfb76d0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xc1803f0 V:|-(45)-[UIWebView:0xc06b020] (Names: '|':UIViewWebViewContainer:0xbfb76d0 )>
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.
2013-11-05 21:32:16.334 emptyproject[16471:70b] web view container (Drawer.m handleLogoutScheme line 938) origin x = 0.000000, origin y = 20.000000, width = 320.000000, height = 548.000000 in the coordinate system of web view container's superview
2013-11-05 21:32:16.336 emptyproject[16471:4707] void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
2013-11-05 21:32:24.822 emptyproject[16471:70b] web view container (Drawer.m handleLoginScheme line 860) origin x = 0.000000, origin y = 20.000000, width = 320.000000, height = 548.000000 in the coordinate system of web view container's superview
2013-11-05 21:32:24.823 emptyproject[16471:70b] [[_webViewContainer webView] constraints] = (
"<NSLayoutConstraint:0xc168600 V:|-(0)-[_UIWebViewScrollView:0xbfb8220] (Names: '|':UIWebView:0xc06b020 )>",
"<NSLayoutConstraint:0xc17d780 _UIWebViewScrollView:0xbfb8220.bottom == UIWebView:0xc06b020.bottom>",
"<NSLayoutConstraint:0xc17db20 H:|-(0)-[_UIWebViewScrollView:0xbfb8220](LTR) (Names: '|':UIWebView:0xc06b020 )>",
"<NSLayoutConstraint:0xc17dce0 _UIWebViewScrollView:0xbfb8220.right == UIWebView:0xc06b020.right>",
"<NSAutoresizingMaskLayoutConstraint:0xc17dfa0 h=--& v=-&- _UIWebViewScrollView:0xbfb8220.midX == + 160>",
"<NSAutoresizingMaskLayoutConstraint:0xc17e0c0 h=--& v=-&- H:[_UIWebViewScrollView:0xbfb8220(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0xc17e0f0 h=--& v=-&- _UIWebViewScrollView:0xbfb8220.midY == UIWebView:0xc06b020.midY>",
"<NSAutoresizingMaskLayoutConstraint:0xc17e220 h=--& v=-&- _UIWebViewScrollView:0xbfb8220.height == UIWebView:0xc06b020.height>"
)
答案 0 :(得分:2)
默认情况下,以编程方式创建的视图会将“AutoresizingMaskIntoConstraints”转换为“是”。这意味着视图上的自动调整遮罩会产生完全确定视图位置的约束。您在视图上设置的任何约束都可能与自动调整约束冲突,因此您必须首先关闭此属性。 IB将为您关闭它。
因此在添加新约束之前将setAutoresizingMaskIntoConstraints设置为NO:
[[[_webViewContainer webView] scrollView] setTranslatesAutoresizingMaskIntoConstraints:NO];