【发布时间】:2018-01-17 05:55:58
【问题描述】:
我是一名初学者 iOS 程序员并尝试开发 iOS 应用程序。我有几个按钮放在应用程序的主页上,如图所示。
请看红圈。图像和标题之间没有空格(填充/插入)。
实际上,我已经使用此代码设置了图像和标题插图。但是,它并没有给出任何不同的结果。
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。请看我的代码。
【问题讨论】:
-
为什么不在
UIImageView和UILabel之间添加标准距离约束?只需使用新约束更新您的自定义单元格。这是自动布局最擅长的。 -
为什么不使用默认的 UIButton?有了它,您可以通过 Storyboard 编辑插图。
-
这是一个普通的 uibutton
-
您是否在按钮上设置了
width约束?您能否(暂时)将按钮的背景颜色设置为.green并发布新图像,以便我们可以看到实际的帧?
标签: ios swift xcode uibutton padding