How can I addSubview to UIButton in Interface Builder

て烟熏妆下的殇ゞ 提交于 2019-12-10 00:54:06

问题


Like I said, I add a UIButton in Interface Builder, I want add a UILabel、UIImageView as the button's subviews, but I can't add any object on it in IB. IS anybody know how to do this? Thank you very much.

I can use code to achieve that, but I want achieve it in IB, so I can use it in any class I want.


回答1:


I have done this before by adding a UIView (instead of the UIButton) then add the UIButton and UILabel to the UIView. The tap from the UIButton still works and you have a label too. You can also add anything else like this.




回答2:


I normally achieve this by

  1. Create a new UIView in Interface Builder and set it to be the same size and location that you want your button to have
  2. Add your subviews to that UIView
  3. Set the UIView's class to UIButton in the Indentity Inspector

Your UIView now works exactly like a UIButton




回答3:


instead of UIButton, add the UILabel and UIImageView to UIView and then add a tap action to it.

EDIT 1:

it's easy to change make a highlighted color effect, use the following code:

- (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];
}



回答4:


Make your UIButton custom button, then add the UILabel on it,then the you will benefit from the UIButton properties and actions,hope this help you



来源:https://stackoverflow.com/questions/13374860/how-can-i-addsubview-to-uibutton-in-interface-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!