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