【问题标题】:Programmatically UIButton in ScrollView以编程方式在 ScrollView 中的 UIButton
【发布时间】:2013-11-09 09:56:32
【问题描述】:

我使用以下代码设置按钮。它工作正常,但在可滚动的窗口中设置它时,按钮仍固定在屏幕上,视图在其后面滚动。 如何使按钮跟随滚动。 如果我使用 IB 设置一个按钮,它会跟随滚动。这就是我想要的,但现在我希望使用编程方法。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

【问题讨论】:

  • 将按钮添加到 UIScrollView no to self.view.

标签: ios uiscrollview uibutton


【解决方案1】:

您将button 添加到UIViewController 的视图中。 修改代码如下:

[self.view addSubview:button];

关注:

[scrollView addSubview:button];

【讨论】:

  • 工作正常。当我想从 plist 在按钮中设置图片时,为我解决了一个问题。
【解决方案2】:

UIButton 添加到滚动视图而不是视图控制器的视图。例如,替换:

[self.view addSubview:button];

[myScrollView addSubview:button];

【讨论】:

    猜你喜欢
    • 2014-11-21
    • 1970-01-01
    • 2022-10-19
    • 2012-02-13
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2014-07-28
    • 2012-02-04
    相关资源
    最近更新 更多