如何将两个 UIBarButtonItem 添加到 UINavigationItem?

发布于 2024-09-04 13:16:37 字数 62 浏览 3 评论 0原文

我想要 UINavigationBar 上有两个 rightBarButtonItem。我怎样才能做到这一点?

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?

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

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

发布评论

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

评论(1

当梦初醒 2024-09-11 13:16:37

您可以将 UISegmentedControl 与两个按钮并将其配置为 momentary 属性设置为 YES。

这是邮件应用程序中用于转到下一条/上一条消息的内容。

更新

为了分配 UISegmentedControl]1 作为右键,您必须将其包装在 UIBarButtonItem 内(示例代码取自 导航栏示例应用程序):

- (void)viewDidLoad
{
    // "Segmented" control to the right
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"up.png"],
                                                    [UIImage imageNamed:@"down.png"],
                                                 nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

    self.navigationItem.rightBarButtonItem = segmentBarItem;
    [segmentBarItem release];
}

You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.

This is what is used in the Mail application to go to next/previous message.

Update

In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):

- (void)viewDidLoad
{
    // "Segmented" control to the right
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"up.png"],
                                                    [UIImage imageNamed:@"down.png"],
                                                 nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

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