【问题标题】:Adding UISegmentedControl to UINavigationController's Toolbar with Storyboard使用 Storyboard 将 UISegmentedControl 添加到 UINavigationController 的工具栏
【发布时间】:2015-11-20 08:56:36
【问题描述】:

我正在尝试使用 xCode 中的 IB 将 UISegmentedControl 添加到 UIToolbar。

虽然使用 xib 文件很容易(创建视图,将工具栏添加到屏幕底部并将 UISegmentedControl 拖到工具栏上),但使用故事板似乎“不可能”。

我在 UINavigationController 中嵌入了一个 UIViewController 场景...

我在 NavigationController 上有以下设置...

我似乎能够将 UIBarButtonItems 添加到 ViewController 中的工具栏,而不是像 xibs 那样的 UISegmentedControl。

通过代码似乎不可能做到...

let segmentControl = UISegmentedControl(items: ["One","Two"]);
let segmentedControlButtonItem = UIBarButtonItem(customView: segmentControl);
let barArray = [segmentedControlButtonItem];
self.navigationController?.setToolbarItems(barArray, animated: false);

这肯定可以通过故事板实现吗?我可以将 UISegmentControls 添加到导航控制器的顶部栏(通过 UIView),但不能添加到底部工具栏。

如何做到这一点?

【问题讨论】:

    标签: ios xcode uinavigationcontroller storyboard uisegmentedcontrol


    【解决方案1】:

    你需要设置你自己的ViewController的toolbarItems,这会传播到NavigationController:

    self.toolbarItems = barArray
    

    我还没有找到通过 Storyboards 添加 SegmentedControl 的方法,我也更喜欢这种方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多