具有n个按钮的水平ScrollView:[iOS] [英] Horizontal ScrollView having n number of buttons : [iOS]

查看:35
本文介绍了具有n个按钮的水平ScrollView:[iOS]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在顶部创建水平滚动视图,如具有许多按钮(例如20个按钮)的滚动菜单?请帮忙!

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

我正在使用以下代码:

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.

推荐答案

我尝试解决此问题,可能是解决了您的问题

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:

ScreenShot:

编码愉快...

这篇关于具有n个按钮的水平ScrollView:[iOS]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆