2014-12-08 83 views
0

我有一組圖像(拇指)在UITableviewcell中。當點擊每個圖像時,將會顯示一個彈出窗口(自定義視圖),這是一個UISCrollview。我在滾動視圖中添加所有圖像(大)。因此用戶可以滾動查看圖像。如何正確應用自動佈局

我將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. 

當我與斷點檢查,我得到縱向和橫向上的UIWindow大小分別爲768和1024。當我將畫面從縱向旋轉到橫向時,「imageScrollView」的大小是768(寬度)和1024(高度),而不是1024x768。什麼是真正的原因?我該如何解決它。

+0

我想你已經添加對您的滾動視圖的兩個約束,用於垂直和水平對齊。 – 2014-12-08 13:28:52

回答

1

這裏發生了幾個不同的衝突事件。

首先,將子視圖添加到根視圖控制器的視圖可能不會工作,因爲它打破了幾個級別的封裝。相反,提出一個新的視圖控制器包含您的scrollView - 無論是作爲一種模式,或從self.navigationController推 - 直接從您的表格單元格。

其次,對UIScrollviews的自動佈局約束有點違反直覺。有關如何設置它們的說明,請參閱https://developer.apple.com/library/ios/technotes/tn2154/_index.html

第三,您可能需要在此新viewController中將translatesAutoresizingMaskIntoConstraints設置爲FALSE。 (它解釋說,在文檔,太)

此外,使用自動佈局看到我的樣本項目中如何把圖像和其它事物的滾動視圖內的一些例子:

https://github.com/annabd351/AutolayoutTemplate