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

时间:2019-01-25 23:32:01

标签: ios swift uibutton

我有一个自定义按钮,其标题在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似乎没有接受限制,附有屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:0)

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

debug view hierarchy

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

enter image description here