[英]why is the order of tint colour for all the segments reversed in UISegmentedControl? how to correct it?
I can't find what is wrong with the index in this code cuz when I run the simulator all the colour of different elements are reversed? 当我运行模拟器时,我找不到在此代码cuz中的索引有什么问题,所有不同元素的颜色都反转了吗?
My ViewController3.h file is as follows: 我的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
here is my ViewController3.m: 这是我的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
The last subview of UISegmentedControl is on left UISegmentedControl的最后一个子视图在左侧
let x = UISegmentedControl(items: ["Red", "Green", "blue"])
x.subviews.last?.frame //(0, 0, 51, 29)
NSString *value = [yourcolorsSegmentName titleForSegmentAtIndex:yourcolorsSegmentName.selectedSegmentIndex];
NSLog(@"%@", value);
if ([value isEqualToString:@"Red"]) {
//set tint color...
} else if ([value isEqualToString:@"Green"]) {
//set tint color...
}
write your code like in this approach .... this will will give perfect output to you.... 像这样编写代码....这将为您提供完美的输出....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.