如何在右侧的UIButton上添加图像,而contentMode不适用于图像?

时间:2018-09-11 07:39:09

标签: ios swift xcode

当我将图像(通过情节提要)添加到按钮图像内容模式不起作用时。当我通过图像插图和标题插图将其修复(iPhone)在另一台设备(iPad)上修复时,其丢失预期比率是多少?我该如何解决?

3 个答案:

答案 0 :(得分:1)

您可以将UIButton的Semantic设置为Force Right-To-Left,以在右侧设置图像。默认SemanticUnspecified

情节提要中的“语义”属性是一条规则,可让iOS知道是否应该翻转视图。 -Apple's Documentations

enter image description here

答案 1 :(得分:0)

您可以尝试以编程方式进行设置。例如,如果设备是iPad或iPhone,则以编程方式设置插图。例如button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)。在viewWillAppearviewDidLoad中尝试此代码。我使用0只是为了理解,请使用您自己的测量值。

答案 2 :(得分:0)

尝试此代码并根据需要修改EdgeInset,然后将出口分配为DropdownButton而不是IUButton

class DropdownButton: UIButton {

override func layoutSubviews() {
    super.layoutSubviews()
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left

    if self.imageView?.image != nil {
        // Move icon to right side
        self.imageEdgeInsets = UIEdgeInsets(
            top: 0,
            left: self.bounds.size.width - self.imageView!.image!.size.width - 10,
            bottom: 0,
            right: 0)
        // Move title to left side
        self.titleEdgeInsets = UIEdgeInsetsMake(5, -self.imageView!.frame.size.width + 10, 5, 5)
    }
}
}