導(dǎo)航欄按鈕 它掂。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回去上一級(jí)
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(leftClick)];
self.navigationItem.leftBarButtonItem= leftItem;
//封裝
[self creatNinePatch];
[self createSegmentControl];
}
//封裝方法
-(void)creatNinePatch {
NSArray *array = @[@"德瑪西亞",@"蓋倫",@"德邦",@"大嘴",@"kelasi",@"艾希",@"寒冰",@"提莫",@"藍(lán)buff"];
//單層for循環(huán)實(shí)現(xiàn)九宮格
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];
//取出數(shù)組內(nèi)容
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);
//寫默認(rèn)關(guān)閉的行為
sc.selectedSegmentIndex= 0;
UIView *titleView = [[UIView alloc]init];
// ???//sc加到titleView上
// ???[titleView addSubview:sc];
// ???titleView.backgroundColor = [UIColor redColor];
//
//把titleView加到導(dǎo)航欄上
//導(dǎo)航欄上的navigationItem
self.navigationItem.titleView= sc;
//self.navigationItem.title = @"1111";
[sc addTarget:self action:@selector(SegmentControlAction:) forControlEvents:UIControlEventValueChanged];
}
-? (void)SegmentControlAction:(UISegmentedControl*)sc {
//switch語(yǔ)句又稱作開(kāi)關(guān)語(yǔ)句當(dāng)case :常量表達(dá)式時(shí)會(huì)執(zhí)行相應(yīng)的方法{}
switch(sc.selectedSegmentIndex) {
case0: {
NSLog(@"消息");
[self creatNinePatch];
}
break;
case1: {
NSLog(@"電話");
//for (*數(shù)組中的元素的類型* in *數(shù)組*)遍歷
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];
}