本文介绍了UIToolBar中的UISegmentedControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何从IB内部将 UISegmentedControl
添加到 UIToolBar
,但是我试图通过编程方式进行相同的操作,因为我使用的是 UISegmentedControl
与没有XIB.
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.
这是 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);
我当时正在考虑做类似 [self.toolbar addSubview:navSC]
的操作,但这没显示任何内容.
I was thinking of doing something like [self.toolbar addSubview:navSC]
, but that didn't show anything.
推荐答案
您需要使用 UIToolbar
方法 – setItems:animated:
(在文档):
You need to use the UIToolbar
method – setItems:animated:
(detailed in the documentation):
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];
这篇关于UIToolBar中的UISegmentedControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!