2015-03-13 77 views
1

我正在使用故事板爲我的應用程序構建ui並使用swift &我準備了下面的屏幕截圖中可以看到的佈局。 enter image description here如何在iOS中使用swift重新定位視圖?

我想隱藏包含stop1的詳細信息的視圖&在運行時將位置stop2視圖替換爲stop1。

我使用下面的代碼隱藏停止1停止2的&更新約束

stop1Frame.hidden = true 
    stop2Frame.updateConstraints() 

請幫助我。

將stop1的高度約束設置爲0,但在控制檯中顯示下面的錯誤。

2015-03-13 17:03:52.738 GroundSpan[5392:1777378] 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) 
    (
    "<NSLayoutConstraint:0x7a644500 V:[UILabel:0x7a643d50'Please tap on + button to...'(63)]>", 
    "<NSLayoutConstraint:0x7a64cb80 V:[UIView:0x7a63aaa0(0)]>", 
    "<NSLayoutConstraint:0x7a64cc10 UILabel:0x7a632e20'Stop1'.top == UIView:0x7a63aaa0.topMargin>", 
    "<NSLayoutConstraint:0x7a64d2b0 V:[UILabel:0x7a643d50'Please tap on + button to...']-(20)-| (Names: '|':UIView:0x7a63aaa0)>", 
    "<NSLayoutConstraint:0x7a64d310 V:[UILabel:0x7a632e20'Stop1']-(NSSpace(8))-[UILabel:0x7a643d50'Please tap on + button to...']>") 

Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0x7a644500 V:[UILabel:0x7a643d50'Please tap on + button to...'(63)]> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2015-03-13 17:03:52.739 GroundSpan[5392:1777378] 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) 
(
    "<NSLayoutConstraint:0x7a64cb80 V:[UIView:0x7a63aaa0(0)]>", 
    "<NSLayoutConstraint:0x7a64cc10 UILabel:0x7a632e20'Stop1'.top == UIView:0x7a63aaa0.topMargin>", 
    "<NSLayoutConstraint:0x7a64d2b0 V:[UILabel:0x7a643d50'Please tap on + button to...']-(20)-| (Names: '|':UIView:0x7a63aaa0)>", 
    "<NSLayoutConstraint:0x7a64d310 V:[UILabel:0x7a632e20'Stop1']-(NSSpace(8))-[UILabel:0x7a643d50'Please tap on + button to...']>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7a64d310 V:[UILabel:0x7a632e20'Stop1']-(NSSpace(8))-[UILabel:0x7a643d50'Please tap on + button to...']> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
+0

你的Swift代碼在哪裏? – Raptor 2015-03-13 11:17:38

+0

swift代碼補充說,我用來隱藏和更新約束 – Pinakin 2015-03-13 11:21:48

回答

2

創建一個IBOutlet到stop1的高度約束,以便您可以在代碼中訪問它。

要隱藏stop1,請將其高度限制設置爲0,以便stop2向上移動。

@IBOutlet weak var stop1HeightConstraint: NSLayoutConstraint! 
... 

// in your function: 
self.stop1HeightConstraint.constant = 0 

UIView.animateWithDuration(0.4, animations: { 
    self.view.layoutIfNeeded() 
}) 

這一切都假設您已正確設置了自動佈局和約束條件。

+0

你的訣竅使stop2重新定位stop1的位置,但在控制檯中給出錯誤請查看編輯問題的錯誤日誌。 – Pinakin 2015-03-13 11:40:36

+0

這就是我的答案的最後一句話:你的約束條件需要正確設置。我們無法看到您如何設置約束條件。白色框應該是UIViews,它們中的標籤和按鈕應該相對於該父視圖定位。 – zisoft 2015-03-13 11:46:32

+0

我沒有看到任何紅色或黃色的線條每個東西都完美的藍色,這意味着所有的約束都設置得很好。 但仍然得到這個味精。 – Pinakin 2015-03-13 11:52:40

相關問題