3

我需要更改使用自动布局的 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 的位置呢?也许禁用自动布局?

4

3 回答 3

4

如果您使用的是 Autolayout,那么您UIViews的定位基于它们的NSLayoutConstraints.

要移动它们,而不是设置它们,frames您需要更改约束。

于 2013-08-16T08:53:38.550 回答
1

可以按照此处所述禁用子视图的自动布局:我可以在运行时禁用特定子视图的自动布局吗?

另一种选择是创建两个故事板,一个打开自动布局,另一个关闭自动布局。然后通过检查设备系统版本来加载相应的故事板(假设您的目标 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];
于 2013-08-16T10:55:39.447 回答
0

我正在开发一个使用自动布局启动的项目,但我需要动态调整代码中的一些帧。这是我所做的,以免受到自动生成的运行时约束的影响(由自动布局提供):

1) 不要在界面构建器中布置视图或组件。

2) 从 alloc/init 开始纯粹以编程方式添加您的视图并适当地设置它们的框架。

3) 完成。

希望有帮助!

附言。您还可以尝试从视图中剥离约束:

[查看 removeConstraints:view.constraints] 但我对纯代码方法有更多的运气。

于 2014-06-19T19:13:17.757 回答