2017-08-09 55 views
1

我是初学iOS程序员,尝试开发iOS应用程序。我有几个按钮,像图片一样放在应用程序的主页上。如何在Swift中为UIButton设置标题填充(插图)

Front Page

请看红色圆圈。图像和标题之间没有空格(填充/插入)。

实际上,我已经使用此代码设置了图像和标题插图。但是,它没有给出任何不同的结果。

buttonDoctor.titleLabel?.numberOfLines = 2 
    buttonDoctor.titleLabel?.lineBreakMode = .byWordWrapping 
    buttonMedical.titleLabel?.numberOfLines = 2 
    buttonMedical.titleLabel?.lineBreakMode = .byWordWrapping 
    buttonQuestion.titleLabel?.numberOfLines = 2 
    buttonQuestion.titleLabel?.lineBreakMode = .byWordWrapping 
    buttonLocation.titleLabel?.numberOfLines = 2 
    buttonLocation.titleLabel?.lineBreakMode = .byWordWrapping 
    buttonPromotion.titleLabel?.numberOfLines = 2 
    buttonPromotion.titleLabel?.lineBreakMode = .byWordWrapping 
    buttonDoctor.image(for: .normal) 
    buttonMedical.image(for: .normal) 
    buttonQuestion.image(for: .normal) 
    buttonLocation.image(for: .normal) 
    buttonPromotion.image(for: .normal) 
    //buttonDoctor.titleLabel?.adjustsFontSizeToFitWidth = true 
    //buttonPromotion.titleLabel?.adjustsFontSizeToFitWidth = true 
    //buttonLocation.titleLabel?.adjustsFontSizeToFitWidth = true 
    //buttonQuestion.titleLabel?.adjustsFontSizeToFitWidth = true 
    //buttonMedical.titleLabel?.adjustsFontSizeToFitWidth = true 



    let btnimg = try? UIImage(named: "doctorschedule.png") 
    var newimg = imageResize(image: btnimg!!, scaledTo: CGSize(width: 36, height: 36)) 
    buttonDoctor.setImage(newimg, for: .normal) 
    buttonDoctor.contentMode = .scaleAspectFit 
    buttonDoctor.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5) 
    buttonDoctor.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0) 

    let btnimg2 = try? UIImage(named: "medicalcheck.png") 
    var newimg2 = imageResize(image: btnimg2!!, scaledTo: CGSize(width: 36, height: 36)) 
    buttonMedical.setImage(newimg2, for: .normal) 
    buttonMedical.contentMode = .scaleAspectFit 
    buttonMedical.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5) 
    buttonMedical.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0) 

    let btnimg3 = try? UIImage(named: "survey.png") 
    var newimg3 = imageResize(image: btnimg3!!, scaledTo: CGSize(width: 36, height: 36)) 
    buttonQuestion.setImage(newimg3, for: .normal) 
    buttonQuestion.contentMode = .scaleAspectFit 
    buttonQuestion.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5) 
    buttonQuestion.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0) 

    let btnimg4 = try? UIImage(named: "location.png") 
    var newimg4 = imageResize(image: btnimg4!!, scaledTo: CGSize(width: 36, height: 36)) 
    buttonLocation.setImage(newimg4, for: .normal) 
    buttonLocation.contentMode = .scaleAspectFit 
    buttonLocation.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5) 
    buttonLocation.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0) 

    let btnimg5 = try? UIImage(named: "promotion.png") 
    var newimg5 = imageResize(image: btnimg5!!, scaledTo: CGSize(width: 36, height: 36)) 
    buttonPromotion.setImage(newimg5, for: .normal) 
    buttonPromotion.contentMode = .scaleAspectFit 
    buttonPromotion.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5) 
    buttonPromotion.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0) 

我已经尝试了几个插值的值,但它没有给出不同的结果。我的代码有什么问题吗?

请有人帮助我如何解决这个问题。谢谢

注:我还有一个问题。我想在首页的顶部放置图片徽标(请参阅橙色圆圈),但我不知道该如何去做。由于没有属性可以在导航栏中插入图像。 仅供参考,它只是UIViewController内部的常规导航栏,我从故事板中输入(而不是UINavigationController)。如果您不介意,请提供一些关于此的建议。

注意:圆中的所有内容都只是普通的UIBUTTON而不是UIImage和UILabel或UIbutton。请参阅我的代码。

+0

为什么不把'UIImageView'和'UILabel'之间的标准距离约束?只需使用新的约束更新您的自定义单元格。这是自动布局最适合的。 –

+0

你为什么不使用默认的UIButton?有了这个,你可以通过Storyboard编辑你的插图。 – Kingalione

+0

这是一个普通的uibutton – christ2702

回答

0

将约束左右两侧设置为0,文本将居中。

enter image description here

enter image description here

4

尝试此(Xcode中9):

从故事板(界面生成器)的设计中,可以设置嵌入用的UIButton的性质,如本快照。

解决方案: 只为标题设置左侧插入为10。保持其他插图为0(或任何你想要的设置)

enter image description here

+0

我已经这样做了,但结果仍然相同。请参阅我的代码 – christ2702

+0

它不会像您在代码中设置的那样工作,您需要扩展(覆盖)此属性。 – Krunal

+0

尝试从接口生成器,而不是编程 – Krunal