自定义按钮标题在 iPad 上不可见

我有一个自定义按钮,它的标题在 iPhone 上可见,但在 iPad 上不可见。那个小图标和底部边框也是可见的。按钮动作确实有效。不知道为什么标题没有显示在 iPad 上。我如何调试它以找出问题?

我在模拟器上运行它。

以下是代码:

import Foundation
import UIKit

class DropDownButton: UIButton {

    var bottomBorder = UIView()

    enum ImageType: String {
        case grey = "Icons/DropDown/Grey"
        case white = "Icons/DropDown/White"
        case red = "Icons/DropDown/red"

        var image: UIImage? { return UIImage(named: rawValue) }
    }

    var imageType: ImageType = .grey {
        didSet{
            setImage(imageType.image, for: [])
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        // Setup Bottom-Border
        self.translatesAutoresizingMaskIntoConstraints = false

        bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
        bottomBorder.backgroundColor = .lightGreyColour
        bottomBorder.translatesAutoresizingMaskIntoConstraints = false

        addSubview(bottomBorder)

        bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
        bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true // Set Border-Strength

        self.sizeToFit()
        self.setImage(UIImage(named: "Icons/DropDown/Grey"), for: [])
        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: UIScreen.main.bounds.size.width-imageView!.frame.width-16, bottom: 4.0, right: 0)
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageView!.frame.width, bottom: 0, right: imageView!.frame.width)
        self.setTitleColor(.white, for: .normal)
        self.setTitleColor(.greyColour, for: .disabled)
    }

}


class ViewController: UIViewController {

    @IBOutlet weak var sortButton: DropDownButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.sortButton.setTitleAndColour(title: "Most Recent", titleFont: fontBody!, colour: .white)
        self.sortButton.imageType = .white
        self.sortButton.bottomBorder.backgroundColor = .white
    }
}

iPad上似乎不接受约束,附上截图

在此处输入图片说明

丹尼斯·W。

您可以使用debug view hierarchyin 选项Xcode查看构成视图控制器的视图。可视化调试器将帮助您查看SortedButton位置。

调试视图层次结构

然后,您可以查看视图层次结构并找到SortedButton视图并查看按钮上设置的约束。您还可以使用 中的show object inspectorshow size inspector按钮Inspectors Panel查看有关该按钮的其他信息。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章