gpt4 book ai didi

iphone - 在 Interface Builder 中设置 UIBarButtonItem View 后,如何以编程方式修改它?

转载 作者:行者123 更新时间:2023-12-03 20:55:15 25 4
gpt4 key购买 nike

我在 Interface Builder 中创建了一个 UIBarButton 项,并将其链接到类中的 UIBarButton 项属性。在 Interface Builder 中,它是Style = PlainIdentifier = Custom,并且标题为空

在 viewDidLoad 方法的类文件中,我尝试向此 UIBarButtonItem 属性添加自定义 View 。

例如

UISegmentedControl *newButton = [[UISegmentedControl alloc] initWithItems:....];
newButton.momentary = YES;
newButton.segmentedControlStyle = UISegmentedControlStyleBar;
newButton.tintColor = [UIColor .....];

[self.myBarButtonItem setCustomView:newButton];

这会导致什么都没有出现。这是为什么?

我读到,如果我以编程方式创建 UIBarButtonItem:

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];

然后将此 BarButtonItem 添加到工具栏 - 它会起作用。我的问题是我的工具栏上有很多东西,并且这个 UIBarButton 项目需要位于工具栏的最右侧,如果我无法在界面生成器中直接链接到它,那么我必须构建我的所有内容以编程方式获取工具栏项目以获得我需要的布局。

那么,当链接到 IB 中创建的 UIBarButtonItem 时,有没有办法使用 customView 来执行此操作?

谢谢!

最佳答案

要更改 UIToolBar 上的内容,您可以使用它的 items 属性。这应该回答你的问题:How to programmatically replace UIToolBar items built in IB

编辑:

我喜欢使用它们的方式是在 View Controller 类中为每个 uibarbuttonitem 创建一堆 IBOutlet。在 Interface Builder 中,我将第一个(默认)项目通常作为工具栏 subview 等放在工具栏上,其余的我将它们作为顶级项目放在 xib 中(与连接到的 View 相同级别) View Controller 的 .view 属性导出)。这样我就不必以编程方式创建它们。并且仍然隐藏它们以便稍后将它们附加到 uitolbar 上。

如果您使用此方法,如果您使用 ivar IBOutlets(而不是@property IBOutlets)作为 xib 中未连接到 KVC 的所有顶级对象,请不要忘记在顶级 IBOutlet 上调用release合规引用。

关于iphone - 在 Interface Builder 中设置 UIBarButtonItem View 后,如何以编程方式修改它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5653625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com