Reputation: 197
I am interested in creating a menu bar like this:
From what I have been reading, I should be doing a UIScrollView. Are there any examples online that do this with infinite scrolling (wrapping around the menu bar) and the ability to have touch control? (i.e. swipe and it changes the menu bar).
Thanks!
Upvotes: 15
Views: 16805
Reputation: 393
I edited example of @Shrikant K so it fits swift 5:
func createVerticalScroll() {
menuScrollView.frame = CGRect(x: 0, y: 0, width: 80, height: self.view.frame.size.height)
var buttonY: CGFloat = 0
for i in 0..<menuButtons.count {
menuButtons[i].frame = CGRect(x: 0, y: buttonY, width: 100, height: 60)
menuButtons[i].setTitle("Button "+String(i), for: .normal)
menuButtons[i].tag = i
menuScrollView.addSubview(menuButtons[i])
buttonY += menuButtons[i].frame.size.width
menuButtons[i].addTarget(self, action: #selector(menuButtonClicked), for: .touchUpInside)
}
menuScrollView.contentSize = CGSize(width: menuScrollView.frame.size.width, height: buttonY)
menuScrollView.backgroundColor = .green
menuScrollView.alpha = 0.5
view.addSubview(menuScrollView)
}
@IBAction func menuButtonClicked(_ sender: UIButton) {
print("I Clicked a button \(sender.tag)")
}
Upvotes: 0
Reputation: 231
I hope this might help you. This doesn't have more animation but works as you expected.
You can make use of CollectionView in this to scroll Horizontally with menu options as CollectionView Cell. For achieving the left and right swiping we can use UISwapeGuestureRecognizers and can scroll between pages.
Here is how i have implemented it using CollectionView for ScrollableMenu as tabs in the top of the screen and the Pages in the bottom of the menu using Container View.
You can download this project and check for getting idea about the implementation form the below link.
Hope this might help you. This is implemented with lates Swift 4.0 and X-Code 9.2
Happy Coding..
Upvotes: 0
Reputation: 879
Your request is very similar to mine. But I cannot find a good enough solution too. So I decide to create something to do that by myself.
The ACTabScrollView supports some different gestures. And the tabs
and pages
will always scroll synchronously.
Swipe
pages normallyDrag
tabs can quickly move pagesClick
a tab to change to that page
The Infinite Scrolling
feature is not ready yet, but I will keep to implement.
I use the similar UI as examples in this project. I don't know what the app is, but if using it as an example bother anyone. Contact me and I will immediately remove that.
Feel free to give me any suggestion to improve it :)
Upvotes: 6
Reputation: 2018
I hope this code will work for you.
- (void)viewDidLoad
{
[super viewDidLoad];
[self createHorizontalScroll];
}
- (void)createHorizontalScroll
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 80)];
int buttonX = 0;
for (int i = 0; i < 5; i++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, 100, 60)];
[button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
[scrollView addSubview:button];
buttonX = buttonX+button.frame.size.width;
[button addTarget:self
action:@selector(changeView:)
forControlEvents:UIControlEventTouchUpInside];
}
scrollView.contentSize = CGSizeMake(buttonX, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];
}
-(void)changeView:(UIButton*)sender
{
NSLog(@"I Clicked a button %ld",(long)sender.tag);
}
You can set scroll content as much you want and menu buttons.Change the view according to the button click. Here the screen shot of the above code
Upvotes: 4