导航栏按钮 。SegmentControl。九宫格。视图
@implementation firstViewController
//宏定义
#define Width [[UIScreen mainScreen]bounds].size.width
#define Height [[UIScreen mainScreen]bounds].size.height
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
self= [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if(self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad {
[superview DidLoad];
self.view.backgroundColor= [UIColor greenColor];
//self.title = @"消息";
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightClick)];
self.navigationItem.rightBarButtonItem= rightItem;
//UIBarButtonSystemItemPageCurl回去上一级
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(leftClick)];
self.navigationItem.leftBarButtonItem= leftItem;
//封装
[self creatNinePatch];
[self createSegmentControl];
}
//封装方法
-(void)creatNinePatch {
NSArray *array = @[@"德玛西亚",@"盖伦",@"德邦",@"大嘴",@"kelasi",@"艾希",@"寒冰",@"提莫",@"蓝buff"];
//单层for循环实现九宫格
for(inti = 0; i < 9; i ++) {
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((Width- (3 * 60 +20))/2.0+(60+10)*(i%3),(Height-(3*60+20))/2.0+(60+10)*(i/3),60,60)];
btn.backgroundColor= [UIColor purpleColor];
//取出数组内容
NSString *title = array[i];
btn.layer.cornerRadius= 30;
//超出父视图之外的部分不显示
btn.layer.masksToBounds=YES;
[btn setTitle:title forState:UIControlStateNormal];
btn.tag= 100;
[self.view addSubview:btn];
}
}
-(void)createSegmentControl {
NSArray *array = @[@"消息",@"电话",@"视频"];
UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:array];
//sc.backgroundColor = [UIColor redColor];
sc.frame=CGRectMake(0, 0,100, 30);
//写默认关闭的行为
sc.selectedSegmentIndex= 0;
UIView *titleView = [[UIView alloc]init];
// //sc加到titleView上
// [titleView addSubview:sc];
// titleView.backgroundColor = [UIColor redColor];
//
//把titleView加到导航栏上
//导航栏上的navigationItem
self.navigationItem.titleView= sc;
//self.navigationItem.title = @"1111";
[sc addTarget:self action:@selector(SegmentControlAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)SegmentControlAction:(UISegmentedControl*)sc {
//switch语句又称作开关语句当case :常量表达式时会执行相应的方法{}
switch(sc.selectedSegmentIndex) {
case0: {
NSLog(@"消息");
[self creatNinePatch];
}
break;
case1: {
NSLog(@"电话");
//for (*数组中的元素的类型* in *数组*)遍历
for(UIView* view in self.view.subviews) {
if((view.tag= 100)) {
[view removeFromSuperview];
}
}
// for (int i = 0; i < 9; i ++) {
//UIButton *btn = (UIButton *) [self.view viewWithTag:100+i];
//[btn removeFromSuperview];
//}
}
break;
case2: {
NSLog(@"视频");
}
break;
default:
break;
}
}
- (void)rightClick {
secondViewController *vc = [[secondViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
//[self.navigationController pushViewController:vc animated:YES];
}
- (void)leftClick {
[self.navigationController popViewControllerAnimated:YES];
}