【问题标题】:Horizontal scroll with buttons in iOSiOS中带有按钮的水平滚动
【发布时间】:2014-12-12 13:31:22
【问题描述】:

我已经实现了带有按钮的水平滚动,但它似乎只在 5 秒后才开始滚动。它卡住了。我猜是 UI 被屏蔽了。

我有以下代码在我的 ViewDidLoad() 中创建它

 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-100, self.view.frame.size.width, 100)];

    int x = 10;
    for (int i = 0; i < 10; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,10,80,80)];
        [button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
        [button setTag:i];
        [button setBackgroundColor:[UIColor greenColor]];
        [button addTarget:self action:@selector(onClickofFilter:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:button];

        x += button.frame.size.width+10;
    }

    scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
    scrollView.backgroundColor = [UIColor redColor];

    [self.view addSubview:scrollView];

有什么办法可以让它顺利吗?

【问题讨论】:

  • 您在 for 循环之后添加滚动视图,这就是为什么它是延迟的原因,因为 for 循环完成然后加载并设置 button = nil if arc 否则添加按钮后为“[button release]”
  • 比较好用的collection view apple是引入UICollectionView试试吧
  • scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);这行代码可能是错误的。好像应该是 scrollView.contentSize = CGSizeMake(buttonWidth * x, buttonHeight + padding);
  • 我完全忘记了collectionview ....cheee
  • 先添加你的scrollView,然后在scollView上添加所有按钮。

标签: ios objective-c uiscrollview uibutton


【解决方案1】:

在 Swift 4.2 中:

func addHorizontalTeamList() {

    let scrollView = UIScrollView(frame: CGRect(x: 0,y: 0, width: self.view.frame.width, height: 60))
    var frame : CGRect?

    for i in 0..<teamsCategory.count {
        let button = UIButton(type: .custom)
        if i == 0 {
            frame = CGRect(x: 10, y: 10, width: 130, height: 40)
        }else{
            frame = CGRect(x: CGFloat(i * 140), y: 10, width: 130, height: 40)
        }

        button.frame = frame!
        button.tag = i
        button.backgroundColor = .lightGray
        button.rounded(border: 0, radius: button.frame.height/2)
        button.addTarget(self, action: #selector(selectTeam), for: .touchUpInside)
        button.setTitle(teamsCategory[i].name, for: .normal)
        scrollView.addSubview(button)
    }

    scrollView.contentSize = CGSize( width: 840, height: scrollView.frame.size.height)
    scrollView.backgroundColor = .white
    self.view.addSubview(scrollView)
}

@objc func selectTeam() {

}

【讨论】:

    【解决方案2】:
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    
        int x = 0;
    
        NSUInteger value= [userInfo count];
        for (int i = 0; i < value; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.titleLabel.textAlignment = UITextAlignmentCenter;
            [button setFont:[UIFont fontWithName:@"Lato-Medium" size:12]];
            button.layer.borderWidth = 1.0f;
            button.layer.cornerRadius = 10;
            button.layer.borderColor = [UIColor whiteColor].CGColor;
            button.clipsToBounds = YES;
            [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
            [button setTag:i];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor clearColor]];
            button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
            [button addTarget:self action:@selector(onClickSwipeButton:) forControlEvents:UIControlEventTouchUpInside];
            button.frame = CGRectMake((i * 100) + (i*10) + 10, 0, 100, 35);
            button.tag = i;
            x = CGRectGetMaxX(button.frame);
            [scrollView addSubview:button];
    
        }
        scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
        scrollView.backgroundColor = [UIColor clearColor];
        [self.randomView addSubview:scrollView];
    

    【讨论】:

      【解决方案3】:

      这里有 swift 2.2 版本的类似代码供参考。

          let scrollView = UIScrollView(frame: CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.width,35))
          var x = 0
          var frame : CGRect?
      
      
      
          for i in 0..<7 {
      
           let button = UIButton(type: .Custom)
      
              if i == 0 {
      
                  frame = CGRectMake(10, 5, 80, 20)
      
              }else{
      
                  frame = CGRectMake(CGFloat(i * 120), 5, 80, 20)
      
      
              }
      
      
              button.frame = frame!
              button.tag = i
              button.backgroundColor = UIColor.clearColor()
              button.addTarget(self, action: #selector(ProgramacioViewController.changeContentOnButtonSelection(_:)), forControlEvents: .TouchUpInside)
              scrollView.addSubview(button)
      
      
          }
      
      
          scrollView.contentSize = CGSizeMake( 840, scrollView.frame.size.height)
          scrollView.backgroundColor = UIColor.clearColor()
          self.view.addSubview(scrollView)
      

      【讨论】:

        【解决方案4】:

        请检查此代码:

          UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
        
          int x = 0;
          CGRect frame;
          for (int i = 0; i < 10; i++) {
        
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
            if (i == 0) {
              frame = CGRectMake(10, 10, 80, 80);
            } else {
              frame = CGRectMake((i * 80) + (i*20) + 10, 10, 80, 80);
            }
        
            button.frame = frame;
            [button setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
            [button setTag:i];
            [button setBackgroundColor:[UIColor greenColor]];
            [button addTarget:self action:@selector(onClickofFilter:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:button];
        
            if (i == 9) {
              x = CGRectGetMaxX(button.frame);
            }
        
          }
        
          scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
          scrollView.backgroundColor = [UIColor redColor];
          [self.view addSubview:scrollView];
        

        【讨论】:

        • 我一定会检查并回复你
        • 你试过了吗?你可以替换 x = CGRectGetMaxX(button.frame); x = CGRectGetMaxX(button.frame) + 10;在scrollView的末尾添加额外的空间
        • 很好的答案谢谢!
        猜你喜欢
        • 2021-01-05
        • 2022-12-08
        • 2014-04-27
        • 2017-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-08
        • 1970-01-01
        相关资源
        最近更新 更多