2014-10-18 47 views
1

我有一個視圖,我在自動佈局上設置了這個視圖。當我在4s iphone中更改按鈕框架/位置時,我無法更改它。如何在不更改自動佈局屬性的情況下更改ios 4s 備註中的按鈕位置?ios autolayout不會改變按鈕幀的位置

這裏是我的代碼:

CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    if (screenBounds.size.height == 568) 
    { 
     // NSLog(@"Our App is running in iPhone5"); 
    } 
    else 
    { 
     //NSLog(@"Our App is running in iPhone 3GS, 4, 4S"); 
     [btn_next setFrame:CGRectMake(12,100,120,45) ]; 
     [getTime setFrame:CGRectMake(151,361,89,162) ]; 
     [picker_AMPM setFrame:CGRectMake(245,304,55,162)]; 
     [img_blowline setFrame: CGRectMake(0,418,320,6)];   
    } 

回答

1

你可以做到這一點:

translatesAutoresizingMaskIntoConstraints = YES,然後在運行時調用setFrame:被自動轉換成新的限制。

translatesAutoresizingMaskIntoConstraints也會根據視圖的autoresizingMask引起新的約束添加。

有關詳細信息,請參閱 Cocoa Auto Layout Guide中的「採用自動佈局」一節。

0

這是因爲你需要改變按鈕的約束,而不是它的框架。爲此,您需要爲約束創建一個出口,並在代碼中進行更改,並更改約束的屬性constant。代碼將是這樣的:

self.myConstraintX.constant=newX; 
self.myConstraintY.constant=newY; 
[self.view layoutSubviews]; 
[self.containerView layoutSubviews]; 

要注意的一件重要的事情是確保此代碼不重複。