好的,我正在努力做这项工作,但没有成功。 基本上我想添加一个UILabel到UIView并居中。UILabel的SetCenter问题
的代码看起来是这样的:
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
我预期的UILabel将UIView的内居中。 好吧,事实并非如此。它在某种程度上对齐,甚至不接近UIView的中心。
我在做什么错?
我只是试着用你的代码逐字,它对我来说正确工作(标签正确地居中在视图中)。也许别的东西正在干扰它呢? – jnic 2011-03-22 16:04:46
Ooops,我忘了提及自定义UIView将从顶部导航栏中替换titleView属性。我会更新发布的代码。 – TeodorC 2011-03-22 16:08:54
尝试 'UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(customTitleView.frame.size.width/2-size.width/2, customTitleView.frame.size.height/2-size.height/2, size.width)];;' 初始化titleLabel – Seega 2011-03-22 16:11:32