gpt4 book ai didi

ios - 具有 n 个按钮的水平 ScrollView : [iOS]

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:32 25 4
gpt4 key购买 nike

如何在顶部创建一个水平 ScrollView ,就像一个有很多按钮(例如:20 个按钮)的滚动菜单?请帮忙!

我正在使用以下代码:

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;

但没有得到我想要的。

最佳答案

我已经尝试解决这个问题,可能会解决你的问题

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;

截图:

enter image description here

快乐编码...

关于ios - 具有 n 个按钮的水平 ScrollView : [iOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com