Xcode、Obj-c、iOS 5.2 iOS 6.0
次のようなものを使用して下部のツールバーに挿入されているUISegmentedControlがあります。
-(void) makeSegmentedControl {
self.SegControl = [[UISegmentedControl alloc] initWithItems:@[@"Title1",@"Title2"]];
self.SegControl.segmentedControlStyle = UISegmentedControlStyleBar;
[self.SegControl setTarget:self action:@selector(handleSegment) forControlEvents:UIControlEventValueChanged]
[self.SegControl setSelectedSegmentIndex:0];
}
-(void) AddSegmentedControlToToolbar {
UIBarButtonItem *tmp = [UIBarButtonItem new];
[tmp setCustomView:self.SegControl];
[self.navigationController.toolbar setItems@[tmp]];
}
-(void) tintSegmentedControl {
for (int i = 0; i< [self.SegControl.subviews count]; i++) {
[[self.SegControl.subviews objectAtIndex:i] setTint:[UIcolor greenColor];
}
NSArray * sortedViews = [self.SegControl.subviews sortArrayUsingFunction:sortFunction];
[[sortedViews objectAtIndex:self.SegControl.selectedSegmentIndex] setTintColor:[UIColor lightGreenColor]];
for (id view in self.SegControl.subviews) {
[view removeFromSuperView];
}
for (id view in sortedViews) {
[self.SegControl addSubview:view];
}
}
-(void)viewDidLoad {
[self makeSegmentedControl];
[self addSegmentedControlToToobar];
[self tintSegmentedControl];
}
-(void) handleSegment {
[self tintSegmentedControl];
}
私たちのアプリで同様のコードを実行すると、クリックすると緑色に変わる非常に青いUISegmentedControllerが表示されます。いくつか試してみましたが、UISegmentsの色合いは、ビューの読み込みが完了するまで固執しないようです。ここで何が起こっているのか分かりますか?
編集
したがって、私は実際にiOS 6.0シミュレーターを使用していたことがわかり、iOS 6には、ViewDidAppear:Animated:が実行される前にUISegmentedControllerに色を付けることができないという制限があります。私がこの変更を行うとき、これが問題だったと思います。
-(void)viewDidLoad {
[self makeSegmentedControl];
[self addSegmentedControlToToobar];
}
-(void)viewDidAppear {
[NSTimer timerWithTimeInterval:0 target:self selector:@selector(tintSegmentedControl) userInfo:nil repeats:NO];
}
できます。何が起こっているのか100%確実ではありません。もっと簡単な方法はありますか?