UILabel 的 SetCenter 问题
好吧,我正在努力完成这项工作,但没有成功。 基本上我想将 UILabel 添加到 UIView 并将其居中。
代码如下所示:
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 的中心。
我做错了什么?
OK, I'm struggling to make this work but without success.
Basically I want to add a UILabel to an UIView and center it.
The code looks like this:
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];
I expected that the UILabel would be centered within the UIView.
Well, it isn't. It is somehow right aligned and not even close to the center of the UIView.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这里出了问题:
当您将
customTitleView
设置为导航项的标题视图时,customTitleView
的框架将会改变。customTitleView
将不再是 320 像素宽,但titleLabel
的框架将保持不变。因此它将不再居中。你不能将
titleLabel
设置为titleView
吗?我认为它应该自动居中。如果没有,我知道一个更复杂的方法来解决它。发表评论,我会告诉你如何做。编辑:因此,这是在
customTitleView
的框架发生更改后重新对齐titleLabel
的方法。当视图的框架发生更改时,会在该视图上调用
layoutSubviews
。因此,您不需要让customTitleView
成为常规的UIView
,而是需要将其设为继承自UIView
的自定义视图。在该自定义视图中,您覆盖layoutSubviews
并在layoutSubviews
的实现中,确保所有内容都根据新框架 (self.frame< /代码>)。我将把实施留给你。
So here's what's going wrong:
When you set
customTitleView
as the title view of the navigation item the frame ofcustomTitleView
will change.customTitleView
won't be 320 pixels wide anymore, but the frame oftitleLabel
will remain the same. Thus it will no longer be centered.Can't you just set
titleLabel
astitleView
? I think it should be centered automatically. If not, I know a more complicated way to solve it. Drop a comment and I'll tell you how.EDIT: So here is how you realign the
titleLabel
aftercustomTitleView
's frame has changed.When the frame of a view is changed
layoutSubviews
is called on that view. So instead of lettingcustomTitleView
be a regularUIView
you need to make it a custom view that inherits fromUIView
. In that custom view you overridelayoutSubviews
and in your implementation oflayoutSubviews
you make sure everything is aligned the way you want based on the new frame (self.frame
). I'll leave the implementation to you.