繁体   English   中英

如何在Interface Builder中将子视图添加到UIButton

[英]How can I addSubview to UIButton in Interface Builder

就像我说的,我在Interface Builder中添加了一个UIButton,我想添加一个UILabel,UIImageView作为按钮的子视图,但我不能在IB中添加任何对象。 有谁知道怎么做? 非常感谢你。

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

我之前通过添加UIView(而不是UIButton)然后将UIButton和UILabel添加到UIView来完成此操作。 来自UIButton的点击仍然有效,你也有一个标签。 你也可以添加其他任何东西。

我通常会实现这个目标

  1. 在Interface Builder中创建一个新的UIView,并将其设置为您希望按钮具有的相同大小和位置
  2. 将您的子视图添加到该UIView
  3. 在Indentity Inspector中将UIView的类设置为UIButton

你的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];
}

制作你的UIButton自定义按钮,然后在其上添加UILabel,然后你将受益于UIButton属性和动作,希望这有助于你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM