首先我們先導(dǎo)入XLSlideSwitch,
然后我們創(chuàng)建六個viewcontroller,
然后就是在AppDelegate創(chuàng)建導(dǎo)航欄
再然后就是在主視圖里寫代碼了:
先導(dǎo)入那六個viewcontroller的頭文件,然后遵守協(xié)議<XLSlideSwitchDelegate>
{
UIView *SomeView; ///是點(diǎn)擊加號按鈕出現(xiàn)的View
UIButton *Btn; ///按鈕
}
@property (nonatomic , strong)XLSlideSwitch *ScrollView;///滾動視圖
viewDidLoad里寫
///創(chuàng)建一個數(shù)組用來存儲名字
NSArray *TitlesArr = @[@"推薦" , @"熱門" ,@"搞笑" , @"軍事" , @"社會" , @"音樂"];
// !!!!!用來存儲六個類的名字 需要更改!!!!!!
NSArray *ControllersArr = @[@"yidongtongxunViewController" , @"chuanmeiViewController" , @"ruangongViewController" , @"wanggonViewController" , @"yunjisViewController" , @"jianzhuViewController"];
NSMutableArray *ViewControllers = [[NSMutableArray alloc] init];
for (int i = 0 ; i < TitlesArr.count; i ++) {
//字符串創(chuàng)建控制器
UIViewController *VC = [[NSClassFromString(ControllersArr[i])alloc] init];
[ViewControllers addObject:VC];
}
//滾動視圖
_ScrollView = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) Titles:TitlesArr viewControllers:ViewControllers];
_ScrollView.delegate = self;
_ScrollView.itemNormalColor = [UIColor darkGrayColor];
_ScrollView.itemSelectedColor = self.navigationController.navigationBar.tintColor;
_ScrollView.customTitleSpacing = 30;
_ScrollView.moreButton = [self moreButton];
[_ScrollView showInViewController:self];
// [_ScrollView showInNavigationController:self];
//View
SomeView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 150, 104, 140, 200)];
SomeView.backgroundColor = [UIColor orangeColor];
for (int i = 0 ; i < 1; i ++) {
[self.view addSubview:SomeView];
SomeView.hidden = YES;
}
外邊寫
(UIButton *)moreButton{
Btn = [[UIButton alloc] init];
// [button setImage:[UIImage imageNamed:@"channelAdd"] forState:UIControlStateNormal];
[Btn setTitle:@"?" forState:UIControlStateNormal];
[Btn setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
[Btn addTarget:self action:@selector(BtnTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
return Btn;
}(void)BtnTouchUpInside{
// NSLog(@"點(diǎn)擊了添加按鈕");
if (Btn.selected == YES) {
// SomeView.hidden = YES;
Btn.selected = NO;
SomeView.hidden = YES;
}else{
Btn.selected = YES;
SomeView.hidden = NO;
}
}