当前位置: 首页 > 工具软件 > XLSlideSwitch > 使用案例 >

滚动视图XLSlideSwitch

曹渝
2023-12-01

https://github.com/mengxianliang/XLSlideSwitch

引入头文件

#import "XLSlideSwitch.h"

 

签订代理

<XLSlideSwitchDelegate> {

    XLSlideSwitch *_slideSwitch;

}

实现方法

[self buildUI];

方法实现

- (void)buildUI {

    self.view.backgroundColor = [UIColor whiteColor];

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(showNextView)];

    

    //要显示的标题

    NSArray *titles = @[@"今天",@"是个",@"好日子",@"心想的",@"事儿",@"都能成",@"明天",@"是个",@"好日子",@"打开了家门",@"咱迎春风",@"~~~"];

    //创建需要展示的ViewController

    NSMutableArray *viewControllers = [[NSMutableArray alloc] init];

    for (NSInteger i = 0 ; i<titles.count; i++) {

        UIViewController *vc = [self viewControllerOfIndex:i];

        [viewControllers addObject:vc];

    }

    //创建滚动视图

    _slideSwitch = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) Titles:titles viewControllers:viewControllers];

    //设置代理

    _slideSwitch.delegate = self;

    //设置按钮选中和未选中状态的标题颜色

    _slideSwitch.itemSelectedColor = self.navigationController.navigationBar.tintColor;

    _slideSwitch.itemNormalColor = [UIColor darkGrayColor];

    //标题横向间距

    _slideSwitch.customTitleSpacing = 30;

    //更多按钮

    _slideSwitch.moreButton = [self moreButton];

    //显示方法

    [_slideSwitch showInViewController:self];

}

 

- (UIButton *)moreButton {

    UIButton *button = [[UIButton alloc] init];

    [button setImage:[UIImage imageNamed:@"channelAdd"] forState:UIControlStateNormal];

    [button setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];

    return button;

}

 

#pragma mark -

#pragma mark SlideSwitchDelegate

 

- (void)slideSwitchDidselectAtIndex:(NSInteger)index {

    NSLog(@"切换到了第 -- %zd -- 个视图",index);

}

 

#pragma mark -

#pragma mark 自定义方法

- (UIViewController *)viewControllerOfIndex:(NSInteger)index {

    UIViewController *vc;

    switch (index%2) {

        case 0:

//上方的控制器

            vc = [[TableViewController alloc] init];

            break;

        case 1:

            vc = [[CollectionViewController alloc] init];

            break;

        default:

            break;

    }

    return vc;

}

 

- (void)showNextView {

    _slideSwitch.selectedIndex += 1;

}

 

 类似资料: