0

我在将标签定位在视图顶部时遇到了一些麻烦,以便它居中。但是,当我使用日期时,字符串的宽度会有所不同。我还注意到在 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];
4

1 回答 1

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 可以旋转,因此您需要检查框架的宽度或高度是否为正确使用的值。

于 2013-02-03T15:55:51.707 回答