UIBarButton渲染问题

时间:2018-09-18 00:27:45

标签: ios swift uikit

当前,当我使用几个自定义方法来更改按钮图像的位置以将其移至文本右侧并添加一些填充时,我正在实现某种自定义UIBarButtonItem。但是,当我离开屏幕并返回按钮时,文本和图像会被切换和扭曲,如所附图像所示。

enter image description here

enter image description here

第一张图片是当我离开屏幕并返回时按钮的外观。第二张图片是它最初的外观。如果有人看到我没有看到的会导致此渲染问题的任何东西,我已经包含了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")
    }

}

1 个答案:

答案 0 :(得分:0)

customView的类型应为UIView。我怀疑使用更高类型的UIButton会导致问题。在您的customView中,您需要将标题和图像设置为子视图,并根据自己的意愿进行约束。