【问题标题】:Horizontal ScrollView having n number of buttons : [iOS]具有 n 个按钮的水平滚动视图:[iOS]
【发布时间】:2017-02-13 10:51:24
【问题描述】:

如何在顶部创建一个水平滚动视图,例如具有许多按钮的滚动菜单(例如: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;

但没有得到我想要的。

【问题讨论】:

标签: ios objective-c uiscrollview uibutton


【解决方案1】:

不幸的是,我的代码是用 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)
    }
}

【讨论】:

    【解决方案2】:

    我已尝试解决此问题,可能会解决您的问题

    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;
    

    截图:

    编码愉快...

    【讨论】:

      【解决方案3】:

      试试这个:

      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;
      

      【讨论】:

      • button.text 给出错误:- 在 UIButton 类型的对象上找不到文本
      • 嗯,它是一个按钮,固定的。
      • 按钮间距大,如何缩小按钮间距
      • 您应该根据需要更改buttonWidth,并调整buttonX = i * buttonWidth + factor以设置按钮之间的间距。
      【解决方案4】:

      您可以通过自定义 collectionViewCell 使用 collectionView 并将其属性设置如下:

      [collectionView setScrollDirection:UICollectionViewScrollDirectionHorizontal];
      
      [collectionView setPagingEnabled:NO];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-09
        • 2014-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-08
        相关资源
        最近更新 更多