【问题标题】:How to add an image & a system bar button item to a UISegmentedControl?如何将图像和系统栏按钮项添加到 UISegmentedControl?
【发布时间】:2012-05-09 19:30:57
【问题描述】:

我有一个UISegmentedControl 作为导航栏的右栏按钮项。这是通过以下代码实现的...

UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segment1",@"Segment2",nil]];

[segmentedControl addTarget:self action:@selector(segmentClicked) forControlEvents:UIControlEventValueChanged];  //Where segmentClicked is the method for segment click action

segmentedControl.frame = CGRectMake(0, 0, 90, 35);

UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];

[segmentedControl release];

self.navigationItem.rightBarButtonItem = rightBaritem;

[rightBaritem release]; 

上面的代码很好,它将显示一个分段控件,其中包含两个段“Segment1”和“Segment2”。

但我想显示图像而不是 Segment1 和系统栏按钮(比如 UIBarButtonSystemItemAdd)而不是 Segment2

图片可以通过代码插入到Segmented控件中,

UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:@"<image_name.image_type>"],???????,nil]];

但我不知道如何使用UIBarButtonSystemItemAdd 代替???????。

提前致谢..

【问题讨论】:

    标签: iphone uibarbuttonitem uisegmentedcontrol


    【解决方案1】:

    此代码可能会解决您的问题。这解决了我同时面临的问题。但不要认为这是准确的问题。我得到了我需要显示的所需内容。这适用于两个按钮.

    UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
            doneButton.momentary = YES; 
            doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
            doneButton.tintColor = [UIColor blackColor];
            [doneButton addTarget:self action:@selector(Done:) forControlEvents:UIControlEventValueChanged];
            [menustyle addSubview:doneButton];
            [doneButton release];
    
            doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
            doneButton.momentary = YES; 
            doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
            doneButton.tintColor = [UIColor blackColor];
            [doneButton addTarget:self action:@selector(Krishna:) forControlEvents:UIControlEventValueChanged];
            [menustyle addSubview:doneButton];
            [doneButton release];
    

    【讨论】:

    • 我们无法在段控制器中添加标签栏项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多