动态构建的分段控件的导航栏中自动调整大小

发布于 2024-12-02 01:08:55 字数 999 浏览 0 评论 0原文

控制器将 UISegmentedControl 添加到导航栏。分段控件在控制器的 viewDidLoad 方法中添加到导航栏,但实际的分段是在调用 viewDidLoad 后动态创建的。

显示视图时,我无法自动调整段的大小。它们都被压扁了,就像这篇文章中的,尽管该决议不适用于此处。如果在将分段控件添加到导航栏的右侧项目之前添加分段(破坏了代码的动态性质),则它们会自动调整大小,并且在显示视图时看起来很好。

下面是我的代码的精简版本。我缺少什么?

@implementation MyController    

- (void)viewDidLoad {

    // segmentedControl is an ivar
    segmentedControl = [UISegmentedControl alloc] initWithItems:[NSArray array]];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
    self.navigationController.navigationBar.topItem.rightBarButtonItem = barButtonItem;

}

- (void)someMethodCalledAfterViewDidLoad {

    [segmentedControl insertSegmentWithTitle:@"a title"
                                     atIndex:0
                                    animated:NO];
}

@end

A controller adds a UISegmentedControl to a navigation bar. The segmented control is added to the navigation bar in the viewDidLoad method of the controller but the actual segments are created dynamically after viewDidLoad is called.

I can't get the segments to be resized automatically when the view is shown. They are all squished, like in this post, though the resolution doesn't apply here. If the segments are added before the segmented control is added to the right item of the navigation bar (breaking the dynamic nature of the code), they are resized automatically and look fine when the view is shown.

Here's a stripped down version of my code, below. What am I missing?

@implementation MyController    

- (void)viewDidLoad {

    // segmentedControl is an ivar
    segmentedControl = [UISegmentedControl alloc] initWithItems:[NSArray array]];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
    self.navigationController.navigationBar.topItem.rightBarButtonItem = barButtonItem;

}

- (void)someMethodCalledAfterViewDidLoad {

    [segmentedControl insertSegmentWithTitle:@"a title"
                                     atIndex:0
                                    animated:NO];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

擦肩而过的背影 2024-12-09 01:08:55

调用 insertSegmentWithTitle 后调用

[segmentedControl sizeToFit];

After calling insertSegmentWithTitle call

[segmentedControl sizeToFit];

七色彩虹 2024-12-09 01:08:55

我今天遇到了同样的问题 - UISegmentedControl 段最初以适当的可变宽度显示,但没有扩展或收缩以适应新的动态更新标题的长度。

每次更新后向分段控件发送 setNeedsLayout 消息解决了该问题。

[segmentedControl setNeedsLayout];

I had the same problem today - the UISegmentedControl segments were initially displayed with the proper variable widths, but did not expand or contract to fit the length of new, dynamically updated titles.

Sending the segmented control a setNeedsLayout message after each update solved the problem.

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