我使用下面的代码在导航栏中有一个标题标签。我似乎无法将其大小调整到完整视图的宽度。知道如何解决这个问题吗?
我认为self.view.bounds.size.width
会将其调整到视图的整个宽度。
这是我的导航栏标题标签代码,还有一个截图可直观地显示问题。
感谢您的帮助
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = @"someText";
tlabel.font = [UIFont fontWithName:@"DIN-Bold" size:20];
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor = [UIColor colorWithRed:(219/255.0) green:(52/255.0) blue:(31/255.0) alpha:1] ;
tlabel.adjustsFontSizeToFitWidth=YES;
tlabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView=tlabel;
从Paras Joshi改变代码后,右侧正确,但左侧偏离。
答案 0 :(得分:1)
使用我在UILabel
中添加UIView
的下面的代码,然后将该视图设置为titleView ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 48)];
tlabel.text=self.navigationItem.title;
tlabel.text = @"someText";
tlabel.font = [UIFont fontWithName:@"DIN-Bold" size:20];
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor = [UIColor colorWithRed:(219/255.0) green:(52/255.0) blue:(31/255.0) alpha:1] ;
tlabel.textAlignment = UITextAlignmentCenter;
[headerView addSubview:tlabel];
self.navigationItem.titleView = headerView;
或添加为下面的子视图。
[self.navigationController.navigationBar addSubview:tlabel];
答案 1 :(得分:0)
尝试使用:
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainscreen].bounds.size.width, 48)];
可能有效。
答案 2 :(得分:0)
您是否导入了uikit框架。导入它以使其无错误