簡體   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