2016-08-03 66 views
0

我想在界面生成器中創建一個像這樣的按鈕。iOS中的自定義UIButton與文本頂部的居中圖像

enter image description here

我已經使用this SO question作爲參考,但我不能讓像坐文本的頂部。

我只能得到偏心的文本和圖像。

enter image description here

下面是一些設置:

enter image description here

enter image description here

+0

在檢查器中,如果選擇「邊緣」,我們可以選擇圖像並更改邊緣值,使用「左,右,上,下」插入 –

回答

1

你試圖同時使用圖像和在同一個視圖中的標籤(UIButton的視圖)和定位它們,這不是執行這種任務的最佳方式。

而且您不能在界面構建器中的UIButton上添加子類。

我會建議你創建一個UIView來代替,並分別添加與設置圖像的UIImageView,並與「設置」一個UILabel:

enter image description here

之後,你可以添加TapGesturRecognizer的通知你當輕敲已到您的視圖按鈕製作:

func tapSetup() { 
     let tapOnMyViewButton = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTapOnMyViewButton)) 
     self.byViewBtn.addGestureRecognizer(tapOnMyViewButton) 
    } 

    func didTapOnMyViewButton() { 
     //Your logic 
    } 
0

,我經常在的情況下使用這樣是建立在Interface Builder一個UIView,並改變其類UIControl的替代品。然後,您可以添加儘可能多的UIImageView,UILabels等,因爲您希望&將它們全部放在AutoLayout與更多「hacky」內容插入之間。

UIButton是UIControl的子類,您可能要查找的所有功能也可能是UIControl的一部分,特別是-addTarget:action:forControlEvents方法。