【发布时间】:2018-09-06 01:49:20
【问题描述】:
所以我有一个 UIBarbuttonItem,我目前正在根据我已经完成的布局进行设计。
import Foundation
import UIKit
class LocationManager: UIBarButtonItem {
var viewController: MainViewController?
lazy var customButton : UIButton = {
let customButton = UIButton(type: .system)
customButton.setImage(UIImage(named: "downArrow"), for: .normal)
customButton.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, -10)
guard let customFont = UIFont(name: "NoirPro-SemiBold", size: 20) else {
fatalError("""
Failed to load the "CustomFont-Light" font.
Make sure the font file is included in the project and the font name is spelled correctly.
"""
)
}
customButton.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
customButton.titleLabel?.font = customFont
customButton.setTitleColor(UIColor.black, for: .normal)
return customButton
}()
override init() {
super.init()
setupViews()
}
@objc func setupViews(){
customView = customButton
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我正确地完成了同时使用图像和标题的工作,并为按钮设置了图像插图,加载时外观很棒。但是,当我离开屏幕并返回时,似乎一切都乱了套,图像被移回,有时会出现两张图像,其中一张的尺寸会变形。
我缺少的自定义按钮实现有什么问题吗?
【问题讨论】: