美文网首页
顶部按钮

顶部按钮

作者: summerTa | 来源:发表于2018-10-08 11:27 被阅读0次

    #define topButtonWidth100

    @interface informationViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>{

        UIScrollView*_topScrollView;

        NSMutableArray*_mArrayTopButton;

        NSMutableArray*_mArrayTitle;

    }

    @end

    @implementationinformationViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        self.navigationController.navigationBar.hidden = YES;

        self.view.backgroundColor = [UIColor yellowColor];

        _mArrayTitle = [NSMutableArray array];

        _mArrayTopButton = [NSMutableArray array];

        [self setTopScrollView];

    }

    -(void)setTopScrollView{

         _mArrayTitle = [NSMutableArray arrayWithObjects:@"推荐",@"娱乐",@"新闻",@"科技",@"体育",@"热点",nil];

        _topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(12, StatusBarHeight, WIDTH-24, 40)];

        _topScrollView.showsHorizontalScrollIndicator = NO;

        _topScrollView.showsVerticalScrollIndicator = NO;

        _topScrollView.backgroundColor = [UIColor redColor];

        for(NSIntegeri =0; i<_mArrayTitle.count; i++) {

            UIButton*topButton = [[UIButtonalloc]initWithFrame:CGRectMake(i *topButtonWidth,0,topButtonWidth,40)];

          //  [topButton setTitleColor:RGB(151, 194, 229) forState:UIControlStateNormal];

            [topButtonsetTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

            [topButtonsetTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

             [topButtonsetTitle:_mArrayTitle[i] forState:UIControlStateNormal];

             [_mArrayTopButtonaddObject:topButton];

            [_topScrollViewaddSubview:topButton];

            topButton.tag=10+ i;

            [topButtonaddTarget:self action:@selector(clickTopButton:) forControlEvents:UIControlEventTouchUpInside];

        }

         ((UIButton*)_mArrayTopButton[1]).selected=YES;

        [_topScrollView setContentSize:CGSizeMake(_mArrayTitle.count * topButtonWidth, 40)];

        ((UIButton*)_mArrayTopButton[1]).selected=YES;

         [_topScrollView setContentSize:CGSizeMake(_mArrayTitle.count * topButtonWidth, 40)];

        if(_topScrollView.contentSize.width - _topScrollView.frame.size.width <0)

        {

            [_topScrollView setContentOffset:CGPointMake(-(_topScrollView.frame.size.width - _topScrollView.contentSize.width)/2, 0) animated:NO];

        }

        [self.view addSubview:_topScrollView];

    }

    //clickTopButton:

    -(void)clickTopButton:(UIButton*)sender{

        // 设置Button的选中情况

        for(UIButton*buttonin_mArrayTopButton) {

            if(button.selected==YES) {

                button.selected=NO;

            }

        }

        sender.selected=YES;

    }

    @end

    相关文章

      网友评论

          本文标题:顶部按钮

          本文链接:https://www.haomeiwen.com/subject/zrbdaftx.html