我怎样才能在界面生成器中添加子视图到UIButton

就像我说的,我在界面生成器中添加一个UIButton,我想添加一个UILabel,UIImageView作为button的子视图,但是我不能在它上面添加任何对象。 有人知道如何做到这一点? 非常感谢你。

我可以使用代码来实现,但我想在IB中实现,所以我可以在任何我想要的类中使用它。

我已经通过添加一个UIView(而不是UIButton),然后将UIButton和UILabel添加到UIView。 从UIButton的水龙头仍然工作,你也有一个标签。 你也可以添加这样的其他东西。

而不是UIButton,将UILabel和UIImageView添加到UIView,然后添加一个水龙头行动。

编辑1:

使用以下代码可以轻松更改高亮显示的颜色效果:

 - (void) onViewTap:(id)sender { [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ _view.backgroundColor = [UIColor blueColor]; } completion:^(BOOL finished){ _view.backgroundColor = [UIColor whiteColor]; }]; //write tap actions below } - (void)viewDidLoad { _view.userInteractionEnabled = YES; UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onViewTap:)]; [_view addGestureRecognizer:tap]; } 

我通常通过

  1. 在Interface Builder中创build一个新的UIView,并将其设置为您想要的button的大小和位置
  2. 将你的子视图添加到该UIView
  3. 将UIView的类设置为Indentity Inspector中的UIButton

你的UIView现在就像UIButton一样工作

让你的UIButton自定义button,然后添加UILabel,然后你将受益于UIButton属性和操作,希望这可以帮助你