我需要在使用autolayout的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 )>
所以我环顾四周,看到了解决我必须设置的问题 [arrivalTimeLabel 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的位置?也许禁用autolayout?
答案 0 :(得分:3)
如果您使用的是Autolayout,则UIViews
根据NSLayoutConstraints
进行定位。
要移动它们,而不是设置它们frames
,您需要更改约束。
答案 1 :(得分:1)
禁用子视图的自动布局可以按照此处的说明完成:Can I disable autolayout for a specific subview at runtime?
另一种选择是创建两个故事板,一个打开自动布局,另一个打开自动布局。然后通过检查设备系统版本加载相应的故事板(假设您的目标也是早于6的iOS版本)。为此,请将此宏添加到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];
答案 2 :(得分:0)
我正在开发一个使用自动布局启动的项目,但我需要动态调整代码中的一些帧。这就是我所做的不会受到自动生成的运行时限制的影响(自动布局):
1)不要在界面构建器中布置视图或组件。
2)以编程方式从alloc / init开始添加您的视图并适当地设置它们的帧。
3)完成。
希望有所帮助!
PS。您还可以尝试使用以下方法从视图中剥离约束:
[view removeConstraints:view.constraints] 但我对纯代码方法有更多的好运。