【问题标题】:Adding an Image to my UIButton subview将图像添加到我的 UIButton 子视图
【发布时间】:2015-02-23 08:05:13
【问题描述】:

我有一个UIButton 作为子视图添加到我的超级视图中,它只是一个大块,如何将图像添加到我的按钮。

- (void)viewDidLoad
{

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(890, 345 , 100, 100);
button.backgroundColor = [UIColor purpleColor];

[self.collectionView.superview addSubview:button];
[button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}

【问题讨论】:

  • [button addSubview:yourImageView]?
  • 你现在有很多答案。如果其中之一解决了您的问题,请接受。否则评论答案并询问更多信息/告诉您的问题或不兼容;)
  • RTFM!这个问题应该被否决。

标签: ios objective-c uibutton subview


【解决方案1】:

您可以通过以下两种方式添加image in UIButton

1) 借助设置Background Image

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(890, 345 , 100, 100);
    button.backgroundColor = [UIColor purpleColor];
    [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.collectionView.superview addSubview:button];
    [button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];

2) 或者您可以将UIImageView 添加为subview

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(890, 345 , 100, 100);
    button.backgroundColor = [UIColor purpleColor];
    [button setClipsToBounds:YES];
    [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    UIImageView * imgView = [[UIImageView alloc]init];
    [imgView setImage:[UIImage imageNamed:@""]];
    [imgView setFrame:CGRectMake(0, 0, 100, 100)];
    [button addSubview:imgView];    
    [self.collectionView.superview addSubview:button];
    [button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];

【讨论】:

    【解决方案2】:

    使用这个: -setBackgroundImage:forState:

    【讨论】:

      【解决方案3】:

      如果您想要一个“图像作为按钮”,而不仅仅是一个内部带有图像的默认按钮,您可能会寻找类似的东西

      BOOL ipad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
      UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];;
      CGRect frame;
      //universal app 
      if (ipad)
      {
          btn.frame = frame = CGRectMake( x, y, w, h );
      }
      else
      {
          btn.frame = frame = CGRectMake( x, y, w, h );
      }
      btn.titleLabel.backgroundColor = [UIColor clearColor];
      forState:UIControlStateNormal];
      
      [btn setBackgroundImage:[UIImage imageNamed:@"buttonAsImage.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
      [btn setTitle:title forState:UIControlStateNormal];
      

      【讨论】:

        【解决方案4】:

        有不同的方法可以做到这一点。 第一个是您可以将 imageview 添加到您的 superview 并根据需要设置 imageview 的图像。然后,在同一位置添加具有 imageview 大小的按钮并将按钮颜色设置为 clearColor。但是您需要先添加 imageView 否则您的按钮将位于图像下方。

        第二个是默认图片到按钮,使用 setImage:image forState:UIControlStateNormal 或 setBackgroundImage:image forState:UIControlStateNormal。

        【讨论】:

          【解决方案5】:

          知道了,方法如下:

          [button setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal && UIControlStateHighlighted];
          

          【讨论】:

            【解决方案6】:

            使用this method - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

            【讨论】:

              猜你喜欢
              • 2011-10-04
              • 2012-07-20
              • 2013-11-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多