gpt4 book ai didi

ios - UIBarButton 渲染问题

转载 作者:行者123 更新时间:2023-11-30 11:09:04 25 4
gpt4 key购买 nike

目前,当我使用几个自定义方法来更改按钮图像的位置以将其移动到文本右侧并添加一些填充时,我正在实现一些自定义 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")
}

}

最佳答案

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

关于ios - UIBarButton 渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52377314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com