【发布时间】:2011-04-13 18:10:14
【问题描述】:
我在 Interface Builder 中创建了一个 UIBarButton 项并将其链接到类中的 UIBarButton 项属性。在 Interface Builder 中,它是 Style = Plain 和 Identifier = Custom,它的 Title 是空白的。
在 viewDidLoad 方法的类文件中,我正在尝试向此 UIBarButtonItem 属性添加自定义视图。
例如
UISegmentedControl *newButton = [[UISegmentedControl alloc] initWithItems:....];
newButton.momentary = YES;
newButton.segmentedControlStyle = UISegmentedControlStyleBar;
newButton.tintColor = [UIColor .....];
[self.myBarButtonItem setCustomView:newButton];
这会导致 NOTHING 出现。这是为什么呢?
我读到如果我以编程方式创建 UIBarButtonItem:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];
然后将此 BarButtonItem 添加到工具栏 - 它会起作用。我的问题是我的工具栏上有很多东西,这个 UIBarButton 项目需要在工具栏的最右边,如果我不能直接在界面构建器中链接到它,那么我必须构建我的所有工具栏项目以编程方式获得我需要的布局。
那么当链接到在 IB 中创建的 UIBarButtonItem 时,有没有办法使用 customView 来做到这一点?
谢谢!
【问题讨论】:
-
我用我喜欢使用的方法编辑了我的答案,这样我就不再像一个便宜的混蛋了;)
标签: iphone cocoa-touch ios4 uibarbuttonitem uitoolbar