xcode UIToolBar 中的 UISegmentedControl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6460298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISegmentedControl Within UIToolBar
提问by PengOne
I know how to add a UISegmentedControl
to a UIToolBar
from within IB, but I am trying to do the same programmatically, because I am using a custom subclass of UISegmentedControl
with doesn't have an XIB.
我知道如何从 IB中将 a 添加UISegmentedControl
到 a UIToolBar
,但我试图以编程方式执行相同的操作,因为我使用的UISegmentedControl
with的自定义子类没有 XIB。
This is the code for the UISegmentedControl
:
这是代码UISegmentedControl
:
SVSegmentedControl *navSC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"List", @"Calendar", nil]];
navSC.delegate = self;
[self.view addSubview:navSC];
[navSC release];
navSC.center = CGPointMake(160, 70);
I was thinking of doing something like [self.toolbar addSubview:navSC]
, but that didn't show anything.
我正在考虑做类似的事情[self.toolbar addSubview:navSC]
,但没有显示任何内容。
回答by PengOne
You need to use the UIToolbar
method – setItems:animated:
(detailed in the documentation):
您需要使用该UIToolbar
方法– setItems:animated:
(在文档中有详细说明):
UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:navSC];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
[toolBar setItems:[NSArray arrayWithObjects:spaceItem,segItem,spaceItem,nil] animated:YES];
[segItem release];
[spaceItem release];