如何正确应用自动布局

时间:2014-12-08 09:15:22

标签: ios swift uiview autolayout

我在UITableviewcell中有一组图像(拇指)。点击每个图像时,将显示一个弹出窗口(自定义视图),这是一个UISCrollview。我在Scroll View中添加所有图像(大)。因此用户可以滚动查看图像。

我将UISCrollView添加到RootViewController的视图中。这样它就可以覆盖整个屏幕。以下是我的代码

我的代码:

self.mainView = self.superview?.window?.rootViewController?.view
imageScrollView = UIScrollView(frame: CGRectMake(0, 0, self.mainView!.frame.size.width, self.mainView!.frame.size.height))
 imageScrollView.delegate = self
self.mainView.addsubview(imageScrollView)

约束:

self.mainView!.addConstraint(NSLayoutConstraint(item: imageScrollView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.mainView!, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        self.mainView!.addConstraint(NSLayoutConstraint(item: imageScrollView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.mainView!, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))

我在控制台中收到错误:

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:0xab1090 h=--- v=--- H:[UIWindow:0xa724f0(768)]>",
    "<NSLayoutConstraint:0x1133f450 UIScrollView:0x115723a0.centerY == UIView:0xa49fe0.centerY>",
    "<NSAutoresizingMaskLayoutConstraint:0x483cc70 h=--& v=--& UIScrollView:0x115723a0.midY == + 512>",
    "<NSAutoresizingMaskLayoutConstraint:0x115b7290 h=-&- v=-&- UIView:0xa49fe0.width == UIWindow:0xa724f0.width>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1133f450 UIScrollView:0x115723a0.centerY == UIView:0xa49fe0.centerY>

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.
2014-12-08 01:13:27.379 afipad[349:60b] 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:0xab10c0 h=--- v=--- V:[UIWindow:0xa724f0(1024)]>",
    "<NSLayoutConstraint:0x1133f090 UIScrollView:0x115723a0.centerX == UIView:0xa49fe0.centerX>",
    "<NSAutoresizingMaskLayoutConstraint:0x115b6dd0 h=--& v=--& UIScrollView:0x115723a0.midX == + 384>",
    "<NSAutoresizingMaskLayoutConstraint:0x115b72f0 h=-&- v=-&- UIView:0xa49fe0.height == UIWindow:0xa724f0.height>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1133f090 UIScrollView:0x115723a0.centerX == UIView:0xa49fe0.centerX>

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.

当我用断点检查时,我在UI和Landscape中的UIWindow大小分别为768和1024。当我将屏幕从纵向旋转到横向时,“imageScrollView”大小为768(宽度)和1024(高度),而不是1024x768。实际原因是什么?我该如何解决呢。

1 个答案:

答案 0 :(得分:1)

这里发生了几件不同的冲突事件。

首先,将子视图添加到根ViewController的视图可能不会起作用,因为它打破了几个级别的封装。相反,提供一个新的视图控制器,包含你的scrollView - 作为模态或直接从你的表格单元格推送self.navigationController。

其次,对UIScrollviews的自动布局限制有点违反直觉。有关如何设置它们的说明,请参阅https://developer.apple.com/library/ios/technotes/tn2154/_index.html

第三,你可能需要在这个新的viewController中将translatesAutoresizingMaskIntoConstraints设置为FALSE。 (它也在文件中解释了)

另外,请参阅我的示例项目,了解如何使用autolayout将图像和其他内容放入scrollView中的示例:

https://github.com/annabd351/AutolayoutTemplate