2016-08-03 62 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方法。