【问题标题】:How to add a button in UIScrollView with addSubview?如何使用 addSubview 在 UIScrollView 中添加按钮?
【发布时间】:2013-09-24 07:59:03
【问题描述】:

我是 iOS 开发新手,我对 UIScrollView 有疑问。首先,我在故事板中创建了一个滚动视图并添加了

@property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;

在视图控制器处理程序中

viewDidLoad 方法中,我有以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
    // You should set these.
    self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    self.mainScroll.minimumZoomScale = .5;  // You will have to set some values for these
    self.mainScroll.maximumZoomScale = 2.0;
    self.mainScroll.zoomScale = 1;

    UIButton* b = [[UIButton alloc] init];
    [b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
    [self.mainScroll addSubview:b];
}

但该按钮没有出现在模拟器中。但是,如果我在 IB 的情节提要上添加一个按钮,则会出现该按钮并且我可以滚动视图。

如何在代码中添加按钮?

【问题讨论】:

  • 您在使用 ARC 吗?如果你不是,你应该是。
  • 确保在按钮框架上。为什么你设置绑定除了 .0f 和 0.f 是按钮的 x 和 y 线。请检查框架大小。

标签: iphone ios objective-c uiscrollview addsubview


【解决方案1】:

试试这个代码

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];

【讨论】:

    【解决方案2】:

    boundsframe 不是一回事。

    改用此代码...

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Hello" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 100, 44);
    [self.mainScroll addSubview:button];
    

    【讨论】:

      【解决方案3】:

      试试这个:

      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [button setTitle:@"Hello" forState:UIControlStateNormal];
      button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
      [self.mainScroll addSubview:button];
      

      【讨论】:

        【解决方案4】:

        从代码中制作按钮的最佳和最简单的方法是:

            UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
            [yourButton setTitle:@"Your Title" forState:UIControlStateNormal];
        
        // if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
            [yourButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
        
        // if you set the button's background image, button's title will be visible 
            [yourButton setBackgroundImage:[UIImage imageNamed:@"yourBackgroundImage.png"] forState:UIControlStateNormal]; 
        
        // you can add target to the button, for handle the event when you touch it.
            [yourButton addTarget:self action:@selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
        
        // then add the button
            [self.view addSubview:yourButton];
        
        
        - (void) handleToucUpInside:(id)sender{
        
            // here you can do anything you want, to handle the action
        
        
        }
        

        【讨论】:

          【解决方案5】:

          您需要设置滚动视图内容的大小,类似于以下代码。

          [self.mainScroll setContentSize:CGSizeMake(width, height)];
          

          【讨论】:

          • 出于某种原因,对我来说,添加 UIImageView 子视图在我这样做之前不会滚动。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-04
          • 2014-08-20
          • 2022-01-22
          • 1970-01-01
          相关资源
          最近更新 更多