簡體   English   中英

如何禁用單個UI元素的自動布局(UIlabel)

[英]How to disable autolayout for individual UI elements (UIlabel)

我需要在使用自動布局的Viewcontroller中更改標簽的位置。 在做之前,我曾經

[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];

到沒有 問題在於,現在標簽無法以編程方式更改位置。

如果您需要編碼以編程方式設置標簽:

    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的位置? 也許禁用自動布局?

如果您使用自動布局,然后你UIViews是根據其定位NSLayoutConstraints

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

可以按照以下說明禁用子視圖的自動布局我可以在運行時為特定子視圖禁用自動布局嗎?

另一種選擇是創建兩個情節提要,一個啟用自動布局,另一個禁用自動布局。 然后通過檢查“設備系統版本”來加載相應的故事板(假設您的目標也是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];

我正在使用自動布局啟動的項目,但是我需要動態調整代碼中的某些框架。 這是我不受自動生成的運行時約束(由自動布局提供)的影響:

1)不要在界面生成器中布置視圖或組件。

2)純粹以編程方式添加您的視圖,從alloc / init開始並適當地設置其框架。

3)完成。

希望有幫助!

PS。 您還可以嘗試使用以下方法從視圖中剝離約束:

[view removeConstraints:view.constraints]但是我對純代碼方法有更多的運氣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM