简体   繁体   English

ios从代码向uiscrollview添加uiview约束

[英]ios add constraints for a uiview to a uiscrollview from code

In my app i have two top level UIViews i created in IB. 在我的应用程序中,我有两个在IB中创建的顶级UIView。 One is the UIViewControllers view, it has a subview which is a UIScrollView. 一个是UIViewControllers视图,它具有一个子视图,即UIScrollView。 The other top level UIView is the content that's going to go in the UIScrollView. 另一个顶级UIView是将在UIScrollView中保留的内容。 That part works fine, i set the UIScrollView's contentsize to the the other UIView's size, and everything works perfectly in portrait mode. 那部分工作正常,我将UIScrollView的contentsize设置为另一个UIView的大小,并且一切都在纵向模式下完美运行。 However if i switch to landscape mode, the UIScrollView resizes nicely, but its subview stays in the top-left corner, but i want it to be in the horizontal center. 但是,如果我切换到横向模式,则UIScrollView会很好地调整大小,但其子视图仍位于左上角,但我希望它位于水平中心。 So i created this constraint and added it to the UIScrollView: 因此,我创建了此约束并将其添加到UIScrollView中:

  NSLayoutConstraint* iPhoneTextFieldContainerAlwaysInHorizontalCenter = [NSLayoutConstraint
                                                                                constraintWithItem:self.iPhoneTextFieldContainerView
                                                                                attribute:NSLayoutAttributeCenterX
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.iPhoneMainScrollView
                                                                                attribute:NSLayoutAttributeCenterX
                                                                                multiplier:1.0
                                                                            constant:0];

However when i switch to landscape mode, the get this constraint exception: 但是,当我切换到横向模式时,得到此约束异常:

( NSAutoresizingMaskLayoutConstraint:0x76dacb0 h=-&- v=-&- UIView:0x76d6eb0.width == UIViewControllerWrapperView:0x76d5b10.width, NSAutoresizingMaskLayoutConstraint:0x76d7e70 h=--& v=--& UIView:0x76d3110.midX == + 160, NSLayoutConstraint:0x76d7030 UIScrollView:0x7680f10.trailing == UIView:0x76d6eb0.trailing, NSLayoutConstraint:0x76d6ff0 H:|-(0)-[UIScrollView:0x7680f10] (NSAutoresizingMaskLayoutConstraint:0x76dacb0 h =-&-v =-&-UIView:0x76d6eb0.width == UIViewControllerWrapperView:0x76d5b10.width,NSAutoresizingMaskLayoutConstraint:0x76d7e70 h =-&v =-&UIView:0x76d3110 160,midX = + NSLayoutConstraint:0x76d7030 UIScrollView:0x7680f10.trailing == UIView:0x76d6eb0.trailing,NSLayoutConstraint:0x76d6ff0 H:|-(0)-[UIScrollView:0x7680f10]

(Names: '|':UIView:0x76d6eb0 ), NSLayoutConstraint:0x76d7330 UIView:0x76d3110.centerX == UIScrollView:0x7680f10.centerX, NSAutoresizingMaskLayoutConstraint:0x76deb70 h=-&- v=-&- UIViewControllerWrapperView:0x76d5b10.width == UINavigationTransitionView:0x76d1ab0.width, NSAutoresizingMaskLayoutConstraint:0x75a3b60 h=--- v=--- V:[UIWindow:0x91979a0(480)], NSAutoresizingMaskLayoutConstraint:0x7595510 h=-&- v=-&- UILayoutContainerView:0x76a5da0.height == UIWindow:0x91979a0.height, NSAutoresizingMaskLayoutConstraint:0x759e020 h=-&- v=-&- UINavigationTransitionView:0x76d1ab0.width == UILayoutContainerView:0x76a5da0.width ) (名称:'|':UIView:0x76d6eb0),NSLayoutConstraint:0x76d7330 UIView:0x76d3110.centerX == UIScrollView:0x7680f10.centerX,NSAutoresizingMaskLayoutConstraint:0x76deb70 h =-&-v =-&-UIViewControllerWrapperView:0x76d5b10。 0x76d1ab0.width,NSAutoresizingMaskLayoutConstraint:0x75a3b60 h = --- v = --- V:[UIWindow:0x91979a0(480)],NSAutoresizingMaskLayoutConstraint:0x7595510 h =-&-v =-&-UILayoutContainerView:0x76a5da0.height == UIWindow: 0x91979a0.height,NSAutoresizingMaskLayoutConstraint:0x759e020 h =-&-v =-&-UINavigationTransitionView:0x76d1ab0.width == UILayoutContainerView:0x76a5da0.width)

Will attempt to recover by breaking constraint NSLayoutConstraint:0x76d7330 UIView:0x76d3110.centerX == UIScrollView:0x7680f10.centerX 将尝试通过打破约束进行恢复NSLayoutConstraint:0x76d7330 UIView:0x76d3110.centerX == UIScrollView:0x7680f10.centerX

Can you tell me what i'm doing wrong here? 你能告诉我我在做什么错吗? Should i remove constraints? 我应该删除约束吗? Can i do such thing? 我可以做这样的事吗?

Thanks is advance! 谢谢是前进!

Sincerely, 真诚的

Zoli 佐利

It looks like your custom constraint and the autoresizing mask constraints are clashing. 看起来您的自定义约束和自动调整大小的蒙版约束发生冲突。 Use this line to avoid that from being generated and add additional constraints as needed. 使用此行来避免生成该行,并根据需要添加其他约束。

self.iPhoneMainScrollView.translatesAutoresizingMaskIntoConstraints = NO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM