【问题标题】:create horizontal scrolling UIButton in iOS using UIScrollView in ios 7在 iOS 7 中使用 UIScrollView 在 iOS 中创建水平滚动 UIButton
【发布时间】:2026-02-03 05:35:01
【问题描述】:

我是 ios 开发的新手,现在想在我的应用程序中提供一个水平滚动按钮,我不知道这是正确的方法,请告诉我如何制作它

这是我尝试以编程方式创建水平 uibutton 的代码,但它现在正在通过警告工作

未声明的选择器newView

我不知道我必须在 newview 中编写代码以使按钮使用四个以上的按钮滚动我希望所有按钮都水平滚动但我无法让我尝试使用一个按钮创建

p>

这是我在视图 didload 中使用的代码

   UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
     scrollview.showsHorizontalScrollIndicator=YES;
     scrollview.userInteractionEnabled=YES;
     scrollview.scrollEnabled=YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(newView:)   forControlEvents:UIControlEventTouchDown];

    [button setTitle:@"Excavations" forState:UIControlStateNormal];
   button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

   [self.view addSubview:scrollview];
   scrollview.contentSize = CGSizeMake(320,960);

谁能告诉我 wt 是在我使用故事板中的 uiscorllview 和按钮时进行水平滚动的正确方法,以及如何使按钮滚动

看到很多类似link 987654321@的教程样本,我不知道让我很困惑

【问题讨论】:

    标签: ios iphone objective-c button uiscrollview


    【解决方案1】:

    按照我的代码:

    CGFloat btnX = 80.0;
    int numberOfButton = 10;
    for (int i = 1 ; i <= numberOfButton; i++)
    {
       UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [button addTarget:self action:@selector(newView:) forControlEvents:UIControlEventTouchDown];
       button.tag = i;
       [button setTitle:@"Excavations" forState:UIControlStateNormal];
       button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
       [scrollview addSubView:button];
       btnX = btnX + 165.0;
    }
    scrollview.contentSize = CGSizeMake(btnX + 50, yourHeight);
    

    您的警告说您忘记添加UIButton 的方法,因此您需要声明按钮的方法,例如..

    -(void) newView:(UIButton *) sender
    {
       //you can access your button by it's tag.
    }
    

    【讨论】:

      【解决方案2】:
      - (void)createScrollViewMenu
      {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
      
          int x = 0;
          for (int i = 0; i < 10; i++) {
              UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
              [button setTitle:[NSString stringWithFormat:@"NameOfBtn %d", i] forState:UIControlStateNormal];
              [button addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchDown];
              button.tag = i;
              [scrollView addSubview:button];
      
              x += button.frame.size.width;
          }
      
          scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
              scrollView.backgroundColor = [UIColor redColor];
      
         [self.view addSubview:scrollView];
      }
      

      这将创建一个高度为 100、宽度与其父级一样大的滚动视图,并为其添加 10 个按钮。 所以你需要声明按钮的方法,比如..

       -(void)BtnClicked:(UIButton *) sender
        {
         //you can access your button by it's tag.
       }
      

      【讨论】: