How to make horizontal scroll menu in iOS

I would like to create a menu that will have horizontal scrolling.

The menu contains only 16 categories. Therefore, I plan to take 8 on the first part and rest 8 on the other part.

Can someone give me an idea of โ€‹โ€‹what needs to be done?

I believe that I need to use below.

UIScrollView Add buttons in this scrollview 

What is it?


I want 8 buttons on the first screen, where the first screen will have two lines with 4 buttons installed on each line.

An example menu can be seen at http://www.shoutem.com/

+6
source share
2 answers

If all you do is add buttons to the horizontal scroll view, you will do something like the following ...

 - (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, a width the size of the parent and add 8 buttons to it.

+22
source

You can achieve your goal with UIScrollView and your UIButton objects, this will be associated with setting each button property / button layout depending on which version of iOS you are planning. (As in Eric's answer).

However, if you customize iOS 6 and above using the UICollectionView, where your items / cells are buttons, you can get horizontal scrolling of the โ€œmenu barโ€ for free. There are many SO posts on this, but the basic idea is to use a stream layout where the element size is tall so that there will be only one row of elements (just make the element height the same as the height of the collection view).

EDIT:

I have to say that this may seem redundant (and perhaps it is), but in the end you will get a much more flexible component in case of changing requirements in the future. It also does not lead to a lot of additional code and abstraction from the tedious details of the layout.

+1
source

Source: https://habr.com/ru/post/951078/


All Articles