邮件应用程序中的UISegmentedControl

时间:2010-09-22 17:52:55

标签: iphone ipad uitoolbar uisegmentedcontrol

如何获得与邮件应用程序中的UISegmentedControl类似的UISegmentedControl,使其与UIToolbar按钮的颜色相同(就好像两个段都处于选定状态一样)。

我想将分段控件用于与Mail完全相同的目的。

(在iPad上,所以灰色不是蓝色)

5 个答案:

答案 0 :(得分:6)

这是来自Apple Sample code ... NavBar的代码以及代码中使用的两个图像。 你应该能够获得与邮件App完全相同的视图。

alt text alt text

// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                                [UIImage imageNamed:@"up.png"],
                                                [UIImage imageNamed:@"down.png"],
                                             nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

defaultTintColor = [segmentedControl.tintColor retain];    // keep track of this for later

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];

答案 1 :(得分:3)

您寻求tintColor财产!

当您使用UISegmentedControl时,您可以将其色调颜色更改为您可以想到的任何颜色。因此,如果您在Interface Builder中添加了UISegmentedControl,那么您可以在- (void)viewWillAppear:(BOOL)animated方法中对其进行样式设置(假设您已将其连接到@synthesized ivar:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // Set the tintColor to match the navigation bar
    self.mySegmentedControl.tintColor = [UIColor colorWithRed:.94 green:.94 blue:.94 alpha:1];

    ... do whatever else in your viewWillAppear ...
}

现在很明显你会想要使用我在上面的示例代码中添加的红色,绿色,蓝色和alpha,但是您可以在字面上为UISegmentedController添加任何您想要的颜色(或者使其像透明一样你想要的,所以这只是找到看起来很完美的RGBA值。

请记住,根据Apple的文档the default value of this property is nil (no color). UISegmentedControl uses this property only if the style of the segmented control is UISegmentedControlStyleBar.

祝你好运!

答案 2 :(得分:1)

我不确切地知道你的意思..但我相信“UISegmentedControlStyleBar”就像segmentedControlStyle一样。

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar

您也可以在IB中设置此属性! (这是被称为“风格”的财产)

答案 3 :(得分:1)

我正在寻找的风格没有记载:它是风格4。 看起来他在这里上下控制:http://media.mobilemeandering.com/wp-content/uploads/2010/04/ipad-mail-message-2.png (不是我的形象顺便说一句)

它基本上使得所有片段都被选中,它用于瞬间推动,并且实际上是多个工具栏按钮被推到一起。 所以它不能在IB中设置,但必须在代码中设置或在nib / xib文件中手动设置,方法是将nib作为文本文件打开。

答案 4 :(得分:0)

我不确定我是否完全明白你要做什么,但我会试一试。

解决方案并不明显,您需要使用UISearchDisplayController才能获得匹配的UISearchBar和UISegmentedControl。

有关示例,请参阅TableSearch示例代码。