2

How to create a horizontal scroll view at the top like a scrolling menu having many buttons (example : 20 buttons) ? Please help !

I am using following code :

CGRect buttonFrame = CGRectMake(0.0f, 0.0f, scrollView.frame.size.width, scrollView.frame.size.height);
    for (NSString *text in wordsArray) {
        UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame];
        button.text = text;
        [scrollView addSubview:button];

        buttonFrame.origin.x +=buttonFrame.size.width;
    }

    CGSize contentSize = scrollView.frame.size;
    contentSize.width = buttonFrame.origin.x;
    scrollView.contentSize = contentSize;

But not getting what i want.

kb920
  • 2,913
  • 2
  • 29
  • 39
shubham mishra
  • 941
  • 1
  • 13
  • 31
  • Please check this answer, I think this is the one, for what you are looking for http://stackoverflow.com/questions/6688160/how-to-programmatically-add-a-uisegmentedcontrol-to-a-container-view/6689592#6689592 – Wolverine Oct 05 '16 at 07:33

4 Answers4

5

I have tried to solve this, might be solve your problem

NSMutableArray *wordsArray = [NSMutableArray arrayWithObjects:@"AAA", @"BBB", @"CCCC", @"dd", @"eeeee", @"ffff", @"g", @"hhh", @"iiiiiii",  @"jjjj", @"kkkkk", @"lllll", nil];

CGFloat btnFrameX = 10.0;
CGFloat Width = self.scrollview.frame.size.width;
CGFloat Height = self.scrollview.frame.size.height;
UIButton *button;
for (NSString *text in wordsArray) {

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor orangeColor]];
    [button.titleLabel setFont:[UIFont systemFontOfSize:20.0f]];
    [button setTitle:text forState:UIControlStateNormal];

    button.frame = CGRectMake(btnFrameX, 20, Width, Height);

    [self.scrollview addSubview:button];

    btnFrameX = btnFrameX + Width + 5;
}

CGSize contentSize = self.scrollview.frame.size;
contentSize.width = wordsArray.count * (Width + btnFrameX);
self.scrollview.contentSize = contentSize;

ScreenShot:

enter image description here

Happy coding...

Sailendra
  • 1,318
  • 13
  • 25
2

Try this:

CGFloat buttonX = 0.f;
CGFloat buttonY = 0.f;
CGFloat buttonWidth = scrollView.frame.size.width;
CGFloat buttonHeight = scrollView.frame.size.height;
for (int i = 0; i < wordsArray.count; i++)
{
    UIButton *button = [UIButton new];
    [button setTitle:wordsArray[i] forState:UIControlStateNormal];
    [scrollView addSubview:button];

    buttonX = i * buttonWidth;
    button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
}

CGSize contentSize = scrollView.frame.size;
contentSize.width = wordsArray.count * buttonWidth;
scrollView.contentSize = contentSize;
CHwang
  • 153
  • 5
2

Unfortunately my code is in Swift, but you will get the gist of it. You can call button.sizeToFit() after you have set the title of the button. That way it will use the preferred width. Take care to use the new width of the button when determining the origin of the next button. Herewith my Swift class.

import Foundation
import UIKit

class ButtonsView: UIView {

    private var scrollView: UIScrollView?

    @IBInspectable
    var wordsArray: [String] = [String]() {
        didSet {
            createButtons()
        }
    }

    private func createButtons() {
        scrollView?.removeFromSuperview()
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
        self.addSubview(scrollView!)
        scrollView!.backgroundColor = UIColor.gray

        let padding: CGFloat = 10
        var currentWidth: CGFloat = padding
        for text in wordsArray {
            let button = UIButton(frame: CGRect(x:currentWidth, y: 0.0, width: 100, height: self.frame.size.height))
            button.setTitle(text, for: .normal)
            button.sizeToFit()
            let buttonWidth = button.frame.size.width
            currentWidth = currentWidth + buttonWidth + padding
            scrollView!.addSubview(button)
        }
        scrollView!.contentSize = CGSize(width:currentWidth,height:scrollView!.frame.size.height)
    }
}

Example

Carien van Zyl
  • 2,705
  • 19
  • 27
1

You can use collectionView using custom collectionViewCell and set its property as below :

[collectionView setScrollDirection:UICollectionViewScrollDirectionHorizontal];

[collectionView setPagingEnabled:NO];
KKRocks
  • 7,878
  • 1
  • 14
  • 81