2013-08-16 74 views
2

我需要在使用自動佈局的Viewcontroller中更改標籤的位置。之前,要做到這一點我用如何禁用單個UI元素的自動佈局(UIlabel)

[arriveTimeLabel setTranslatesAutoresizingMaskIntoConstraints:YES]; 
[departTimeLabel setTranslatesAutoresizingMaskIntoConstraints:YES]; 

但產生一些運行時警告

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. 
2013-08-15 23:25:58.791 Vicinitime[78327:c07] 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:0x9173a20 UIScrollView:0x9580190.bottom == UIView:0x9173890.bottom>", 
    "<NSLayoutConstraint:0x9171fe0 V:|-(138)-[UILabel:0x958cf70] (Names: '|':UIScrollView:0x9580190)>", 
    "<NSLayoutConstraint:0x91739a0 V:|-(0)-[UIScrollView:0x9580190] (Names: '|':UIView:0x9173890)>", 
    "<NSAutoresizingMaskLayoutConstraint:0xab6ec90 h=--& v=&-& UILabel:0x958cf70.midY == -6.3989*UIScrollView:0x9580190.height>", 
    "<NSAutoresizingMaskLayoutConstraint:0xab6eef0 h=--& v=&-& V:[UILabel:0x958cf70(25)]>", 
    "<NSAutoresizingMaskLayoutConstraint:0xab6dbd0 h=--& v=--& V:[UIView:0x9173890(455)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x9171fe0 V:|-(138)-[UILabel:0x958cf70] (Names: '|':UIScrollView:0x9580190)> 

所以我環顧四周,看到了解決這個問題我必須設置 [arriveTimeLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [departTimeLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

to no。問題是,現在標籤沒有以編程方式改變位置。

如果你需要的代碼編程設置標籤:

if(travelTimeInt <= activityTimeInt){ 

     [timeBreakdownButtonImage setBackgroundImage:[UIImage imageNamed:@"timeBreakdownBackground7.png"] forState:UIControlStateNormal]; 
     NSLog(@"got to setting label place"); 

     [self.arriveTimeLabel setFrame:CGRectMake(67, 170, 41, 25)]; 

     [self.departTimeLabel setFrame:CGRectMake(211, 170, 41, 25)]; 



    } 
    if(travelTimeInt * 7 >= activityTimeInt){ 

      [timeBreakdownButtonImage setBackgroundImage:[UIImage imageNamed:@"timeBreakdownBackground6.png"] forState:UIControlStateNormal]; 

      NSLog(@"got to setting label place"); 

      self.arriveTimeLabel.frame = CGRectMake(71, 170, 41, 25); 

     self.departTimeLabel.frame = CGRectMake(207, 170, 41, 25); 

    } 

那麼,有沒有辦法改變的UILabel的位置自動版式上?也許禁用自動佈局?

+1

截至目前,無法爲每個組件單獨禁用自動佈局選項。 –

+0

關於整個viewcontroller禁用? – Spenciefy

+0

是的,您可以禁用整個ViewController。選擇與您的ViewController關聯的筆尖 - >轉到文件檢查器 - >取消選中Interface Builder文檔部分中的「使用Autolayout」。 –

回答

3

如果您使用的是Autolayout,那麼您的UIViews根據它們的NSLayoutConstraints定位。

要移動它們,而不是設置它們的frames,您需要更改約束條件。

1

禁用自動佈局的一個子視圖可以做到這裏提到:Can I disable autolayout for a specific subview at runtime?

另一種選擇是創建兩個故事板,一個與自動佈局,以及其他與自動佈局了。然後通過檢查設備系統版本來加載相應的故事板(假設您的iOS版本早於6)。對於這一點,這個宏添加到您的AppDelegate:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending) 

然後:

UIStoryboard *mainStoryboard = nil; 

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) 
{ 
    mainStoryboard = [UIStoryboard storyboardWithName:@"AutoLayoutStoryboard" bundle:nil]; 
} 
else 
{ 
    mainStoryboard = [UIStoryboard storyboardWithName:@"NoAutoLayoutStoryboard" bundle:nil]; 
} 

//load initial view controller 
UIViewController *rootView = [mainStoryboard instantiateInitialViewController]; 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
self.window.rootViewController = rootView; 
[self.window makeKeyAndVisible]; 
+0

@Spenciefy這兩種解決方案是否適合您? – instaable

0

我的工作,將其與自動佈局發起的一個項目,但我需要動態地調整一些幀碼。這是我爲了不被自動生成的運行時間限制(自動佈局提供)所損害的:

1)不要在界面構建器中設置視圖或組件。

2)純粹以編程方式從alloc/init開始並適當地設置它們的幀來添加你的視圖。

3)完成。

希望有幫助!

ps。你也可以嘗試從視圖剝離約束:

[view removeConstraints:view.constraints] 但我已經有了更純粹的代碼方法的運氣。

相關問題