我在将视图顶部的标签定位时遇到了一些麻烦,因此它居中。但是,因为我使用日期,字符串的宽度会有所不同。我也注意到在3.5英寸屏幕上,文字不合适。我在标签的两边都有两个图像按钮。
所以我需要有一个保证金。
为了使问题更复杂,视图仅以横向显示,因此左侧是顶部等。
ViewController *nextController = [[ViewController alloc]
initWithNibName:@"View" bundle:nil];
CGAffineTransform newTransform = CGAffineTransformMake(0.0,1.0,-1.0,0.0,0.0,0.0);
nextController.view.transform = newTransform;
nextController.hidesBottomBarWhenPushed = YES;
我确实认为我需要做的就是......
int margin = 50;
lblTitle.frame = CGRectMake(margin, 2, th - (margin * 2), 22);
但这不起作用。 它几乎就像是在左侧添加状态栏的高度,但状态栏是可见的。我做了很多尝试,不知道我做错了什么。
lblTitle = [[UILabel alloc] initWithFrame:CGRectZero];
lblTitle.text = [NSString stringWithFormat:@"%@ - from %@ to %@",
strChartType, displayStartDate, displayEndDate];
int th = self.view.frame.size.height;
//int th = self.view.bounds.size.height;
lblTitle.frame = CGRectMake(0, 2, th, 22); // x, y, w, h
//[lblTitle setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin];
//lblTitle.center = CGPointMake( self.view.bounds.size.width / 2, 10);
[lblTitle setTextAlignment:UITextAlignmentCenter];
lblTitle.font = [UIFont boldSystemFontOfSize:14];
lblTitle.adjustsFontSizeToFitWidth = FALSE;
lblTitle.backgroundColor = [UIColor yellowColor];
[self.view addSubview:lblTitle];
答案 0 :(得分:0)
您可以获取屏幕边界,然后减去状态栏的高度以获得标签宽度。
float screenHeight = [UIScreen mainScreen].bounds.size.height;
float statusBarHeight = ([UIApplication sharedApplication].statusBarFrame.size.height < [UIApplication sharedApplication].statusBarFrame.size.width) ? [UIApplication sharedApplication].statusBarFrame.size.height : [UIApplication sharedApplication].statusBarFrame.size.width;
float labelWidth = screenHeight - statusBarHeight;
注意:mainScreen边界始终以纵向模式返回,因此您不必担心方向,但可以旋转statusBarFrame,因此您需要检查框架的宽度或高度是否是要使用的正确值。