0

在 iOS 7 中,尝试添加一些约束以确保 Web 视图在更改 Web 视图的框架后位于其父视图(即以下代码中的 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>"
)
4

1 回答 1

2

默认情况下,以编程方式创建的视图已将 AutoresizingMaskIntoConstraints 转换为 YES。这意味着视图上的自动调整掩码会产生完全确定视图位置的约束。您在视图上设置的任何约束都可能与自动调整大小的约束发生冲突,因此您必须先关闭此属性。IB会为你关闭它。

所以在添加新约束之前将 translatesAutoresizingMaskIntoConstraints 设置为 NO:

[[[_webViewContainer webView] scrollView] setTranslatesAutoresizingMaskIntoConstraints:NO];
于 2013-11-05T13:10:13.057 回答