为什么在UISegmentedControl中所有段的色调颜色顺序颠倒了?怎么纠正呢?

时间:2017-02-10 06:38:53

标签: ios objective-c uisegmentedcontrol tintcolor

当我运行模拟器时,我在这段代码中找不到索引有什么问题cuz所有不同元素的颜色都被反转了?

我的ViewController3.h文件如下:

#import <UIKit/UIKit.h>

@interface ViewController3 : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *image1;
- (IBAction)segmentControl:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *mysegment;
@property (weak, nonatomic) IBOutlet UIView *colorToBeDisplayedInView;



@end

这是我的ViewController3.m:

#import "ViewController3.h"

@interface ViewController3 ()
@property (strong, nonatomic) IBOutlet UIView *segments;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController3




- (void)viewDidLoad {
[super viewDidLoad];
//creating textField programmatically
//    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 20)];
//    textField.borderStyle = UITextBorderStyleRoundedRect;
//    textField.font = [UIFont systemFontOfSize:15];
//    textField.placeholder = @"enter text here";
//    textField.autocorrectionType = UITextAutocorrectionTypeNo;
//    textField.keyboardType = UIKeyboardTypeDefault;
//    textField.returnKeyType = UIReturnKeyDone;
//    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//    [self.view addSubview:textField];

//setting tint color for segments
[[_mysegment.subviews objectAtIndex:0] setTintColor:[UIColor redColor]];

[[_mysegment.subviews objectAtIndex:1] setTintColor:[UIColor blueColor]];

[[_mysegment.subviews objectAtIndex:2] setTintColor:[UIColor greenColor]];

[[_mysegment.subviews objectAtIndex:3] setTintColor:[UIColor purpleColor]];

[[_mysegment.subviews objectAtIndex:4] setTintColor:[UIColor orangeColor]];

//to preload everyview before hand
_label.text=@"1";
_image1.image = [UIImage imageNamed:@"fruits.png"];
_colorToBeDisplayedInView.backgroundColor = [UIColor redColor];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/


- (IBAction)segmentControl:(id)sender {
if (_mysegment.selectedSegmentIndex==0)
{
    _label.text=@"1";
    _image1.image = [UIImage imageNamed:@"fruits.png"];
    _colorToBeDisplayedInView.backgroundColor = [UIColor redColor];
}
else if (_mysegment.selectedSegmentIndex==1)
{
    _label.text=@"2";
    _image1.image = [UIImage imageNamed:@"airplane.png"];
    _colorToBeDisplayedInView.backgroundColor = [UIColor blueColor];
}
else if (_mysegment.selectedSegmentIndex==2)
{
    _label.text=@"3";
    _image1.image = [UIImage imageNamed:@"boat.png"];
    _colorToBeDisplayedInView.backgroundColor = [UIColor greenColor];
}
else if (_mysegment.selectedSegmentIndex==3)
{
    _label.text=@"4";
    _image1.image = [UIImage imageNamed:@"arctichare.png"];
    _colorToBeDisplayedInView.backgroundColor = [UIColor purpleColor];
}else
{
    _label.text=@"5";
    _image1.image = [UIImage imageNamed:@"cat.png"];
    _colorToBeDisplayedInView.backgroundColor = [UIColor orangeColor];
}
}
@end

2 个答案:

答案 0 :(得分:0)

UISegmentedControl的最后一个子视图位于左侧

let x = UISegmentedControl(items: ["Red", "Green", "blue"])
x.subviews.last?.frame    //(0, 0, 51, 29)

答案 1 :(得分:0)

NSString *value = [yourcolorsSegmentName titleForSegmentAtIndex:yourcolorsSegmentName.selectedSegmentIndex];
NSLog(@"%@", value);
if ([value isEqualToString:@"Red"]) {
//set tint color...
} else if ([value isEqualToString:@"Green"]) {
//set tint color...
}

像这种方法一样编写代码.... 这将为您提供完美的输出....