我正在做一个应用程序,我正在创建一个UILabels程序,因为我将创建大约15个不同帧的标签,所以我只使用一个方法并通过参数传递调用它。
当我将其旋转到横向视图时,应该更改标签的位置,因为右边有很多空间留空。所以我试图使用自动调整遮罩但它不起作用,所以请建议我应该做什么这样,当我转向横向时,标签框会改变并适合于正确的位置。请给我一个示例代码。我现在使用的代码如下所示。
{
[self.view addSubview:[self createLabel:CGRectMake(450,140,60,20):@"Ratings:"]];
[self.view addSubview:[self createLabel:CGRectMake(450,170,60,20):@"Reviews:"]];
[self.view addSubview:[self createLabel:CGRectMake(50,170,50,20):@"Hours:"]];
[self.view addSubview:[self createLabel:CGRectMake(50,200,50,20):@"Phone:"]];
self.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
[super viewDidLoad];
}
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
{
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:@"Arial" size:13];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.font = [UIFont boldSystemFontOfSize:13];
myLabel.text = labelTitle;
[myLabel autorelease];
return myLabel;
}
答案 0 :(得分:3)
您还应将autoresizingMask
设置为self.view
的所有子视图 - 例如,添加-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle
方法中的下一行:
myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
或
myLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;