Home » IOS » The properties and usage of UIday0601:UISegmentedC...

The properties and usage of UIday0601:UISegmentedControl

Segment is equivalent to integrating several button


usually we click different Segment to switch different View


addTarget: action: forControlEvents: adds events to UISegmentedControl, controlEvent is UIControlEventValueChanged


AppDelegate.m



- (BOOL), application: (UIApplication *), application, didFinishLaunchingWithOptions: (NSDictionary *), launchOptions {
Self.window = [[UIWindow, alloc], initWithFrame:[[UIScreen, mainScreen], bounds]];
Override point for customization after application / / launch.
Self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc]init];
Self.window.rootViewController = rootVC;
Return YES;
}





RootViewController.m



#import "RootViewController.h""
@interface, RootViewController ()
@property (nonatomic, strong) RootView * rv;
@end
@implementation RootViewController
- - (void) loadView{
Self.rv = [[RootView, alloc]initWithFrame:[UIScreen, mainScreen].bounds];
Self.view = _rv;
}
- - (void) viewDidLoad {
[super viewDidLoad];
Do any additional setup after loading / the view.
/ / add event
[self.rv.segment addTarget:self action:@selector (segmentAction:) forControlEvents:UIControlEventValueChanged];
/ / to select the settings selected by default
Self.rv.segment.selectedSegmentIndex = 2;
The moment / display with the less
Self.rv.segment.momentary = YES;
The width / be adaptive, less adaptive display words according to the label width, the default is NO
Self.rv.segment.apportionsSegmentWidthsByContent = NO;
/ / insert Title animated animation defaults to NO
[self.rv.segment insertSegmentWithTitle:@, "test", "atIndex:0 animated:YES]";
/ / delete section header
[self.rv.segment removeSegmentAtIndex:0 animated:YES];
Set / segment
[self.rv.segment setTitle:@ "0000" forSegmentAtIndex:1];
/ / set the sub width and the width of adjacent segment extrusion
[self.rv.segment setWidth:120 forSegmentAtIndex:1];
/ / offset Title settings, the default is centered
[self.rv.segment setContentOffset:CGSizeMake (10, 10) forSegmentAtIndex:0];
Can use the default YES / / set
[self.rv.segment setEnabled:NO forSegmentAtIndex:0];
Background / Map
[self.rv.segment, setBackgroundImage:[UIImage, imageNamed:@, "101.png" [forState:UIControlStateNormal] barMetrics:UIBarMetricsDefault];
}
- - (void) segmentAction: (UISegmentedControl *) sender{
/ / selected: selectedSegmentIndex index, index segment from the beginning of 0
NSLog (@%ld ', sender.selectedSegmentIndex);
Switch (sender.selectedSegmentIndex) {
Case 0:
NSLog (@ first ');
Break;
Case 1:
NSLog (@ second ');
Break;
Case 2:
NSLog (@ three ');
Break;
Case 3:
NSLog (@ Four ');
Break;
Case 4:
NSLog (@ five ');
Break;
Default:
Break;
}
}
- - (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end


RootView.h



#import 
@interface RootView: UIView
/ / sub controller
@property (nonatomic, strong) UISegmentedControl * segment;
@end


RootView.m



#import "RootView.h""
@implementation RootView
- - (instancetype) initWithFrame: (CGRect) frame
{
Self = [super initWithFrame:frame];
If (self) {
[self p_setupViews];
}
Return self;
}
- - (void) p_setupViews{
Self.backgroundColor = [UIColor yellowColor];
NSArray * array = "first" @[@ @ @ "second", "third"];
/ / create a subsection controller (sub controller initialization)
Self.segment = [[UISegmentedControl alloc]initWithItems:array];
Self.segment.frame = CGRectMake (50, 50, 200, 50);
Modify / control color
Self.segment.tintColor = [UIColor redColor];
[self addSubview:_segment];
}
@end








copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest