如何在顶部创建一个水平滚动视图,就像一个有很多按钮的滚动菜单(例如: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;
但没有得到我想要的东西。
答案 0 :(得分:5)
我试图解决这个问题,可能会解决你的问题
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;
截图:
快乐的编码......
答案 1 :(得分:2)
试试这个:
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;
答案 2 :(得分:2)
不幸的是我的代码在Swift中,但你会得到它的要点。设置按钮标题后,可以调用button.sizeToFit()。这样它将使用首选宽度。在确定下一个按钮的原点时,请务必使用按钮的新宽度。这是我的Swift课程。
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)
}
}
答案 3 :(得分:1)
您可以使用自定义collectionViewCell使用collectionView并将其属性设置如下:
[collectionView setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[collectionView setPagingEnabled:NO];