自动调整大小掩码不适用于旋转标签的对齐

发布于 2024-12-10 19:40:37 字数 1030 浏览 0 评论 0原文

我正在做一个应用程序,我以编程方式创建 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;
}

I am doing an application where i am creating a UILabels programmitically,As i will be creating some 15 labels with different frames so i using only one method and calling it by parameter passing.

when i rotate it to Landscape view the position of the labels should be changed as there will lot of space left empty at right side..so i am trying to use Autoresizing mask but its not working,so please suggest what i should do so that label frame changes and fits at right position when i turn to landscape orientation. please provide me a sample code.The present code i am using is given below.

{
[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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-12-17 19:40:37

您还应该将 autoresizingMask 设置为 self.view 的所有子视图 - 例如,在 -(UILabel*)createLabel:(CGRect)frame 中添加下一行:(NSString*)labelTitle 方法:

myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;

myLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;

You should set also autoresizingMask to all subview of self.view - add, for example, next line in the -(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle method:

myLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;

or

myLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文