UILabel 的 SetCenter 问题

发布于 2024-10-26 02:34:59 字数 1088 浏览 2 评论 0原文

好吧,我正在努力完成这项工作,但没有成功。 基本上我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-11-02 02:34:59

所以这里出了问题:

self.navigationItem.titleView = customTitleView;

当您将 customTitleView 设置为导航项的标题视图时,customTitleView 的框架将会改变。 customTitleView 将不再是 320 像素宽,但 titleLabel 的框架将保持不变。因此它将不再居中。

你不能将 titleLabel 设置为 titleView 吗?我认为它应该自动居中。如果没有,我知道一个更复杂的方法来解决它。发表评论,我会告诉你如何做。

编辑:因此,这是在 customTitleView 的框架发生更改后重新对齐 titleLabel 的方法。

当视图的框架发生更改时,会在该视图上调用 layoutSubviews 。因此,您不需要让 customTitleView 成为常规的 UIView,而是需要将其设为继承自 UIView 的自定义视图。在该自定义视图中,您覆盖 layoutSubviews 并在 layoutSubviews 的实现中,确保所有内容都根据新框架 (self.frame< /代码>)。我将把实施留给你。

So here's what's going wrong:

self.navigationItem.titleView = customTitleView;

When you set customTitleView as the title view of the navigation item the frame of customTitleView will change. customTitleView won't be 320 pixels wide anymore, but the frame of titleLabel will remain the same. Thus it will no longer be centered.

Can't you just set titleLabel as titleView? 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 after customTitleView's frame has changed.

When the frame of a view is changed layoutSubviews is called on that view. So instead of letting customTitleView be a regular UIView you need to make it a custom view that inherits from UIView. In that custom view you override layoutSubviews and in your implementation of layoutSubviews you make sure everything is aligned the way you want based on the new frame (self.frame). I'll leave the implementation to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文