how to make horizontal scrolling menu in iOS

前端 未结 2 976
猫巷女王i
猫巷女王i 2021-02-03 13:48

I would like to make a menu which will have horizontal scrolling.

The menu contains total 16 categories. So I am planning to take 8 on first part and rest 8 on another p

2条回答
  •  无人共我
    2021-02-03 14:22

    If all you're doing is adding buttons to a horizontal scroll view you would do something like follows...

    - (void)createScrollMenu
    {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    
            int x = 0;
            for (int i = 0; i < 8; i++) {
                UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
                [button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
    
                [scrollView addSubview:button];
    
                x += button.frame.size.width;
            }
    
            scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
                scrollView.backgroundColor = [UIColor redColor];
    
           [self.view addSubview:scrollView];
        }
    

    This will create a scrollview with a height of 100, width as big as its parent, and add 8 buttons to it.

提交回复
热议问题