簡體   English   中英

UIButton中的按鈕和圖像對齊問題

[英]Button and Image Alignment issues in UIButton

所以我有一個基於當前布局設計的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")
    }

}

我正確地使用了圖像和標題,並為按鈕設置了圖像插圖,並且在加載時外觀很好。 但是,當我離開屏幕並返回時,似乎一切都亂丟了,圖像又被移回了,有時會有兩張圖像,其中一張的尺寸會變形。

我缺少的自定義按鈕實現有什么問題嗎?

我包含了之前和之后的圖像 在此處輸入圖片說明

在此處輸入圖片說明

在此處輸入圖片說明

我建議您創建自定義按鈕類,然后通過添加子視圖來創建標題和圖像。 在這種情況下,UIImageView和UILabel。 因為UIButton繼承自UIView,所以您可以輕松做到這一點。 使用這種方式我從來沒有遇到過問題。

這是我為您編寫的代碼:

import UIKit

class ViewController: UIViewController {

lazy var customButton: CustomButton = {
    let button = CustomButton(frame: CGRect(x: 50,
                                            y: 200,
                                            width: view.frame.width - 100,
                                            height: 50))
    // This mask for rotation
    button.autoresizingMask = [.flexibleLeftMargin,
                             .flexibleRightMargin,
                             .flexibleTopMargin,
                             .flexibleBottomMargin]
    button.attrTitleLabel.text = "San Francisco, CA"
    button.addTarget(self, action: #selector(chooseCity), for: .touchUpInside)
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .blue
    view.addSubview(customButton)
}

@objc func chooseCity() {
    print("Choose city button has pressed")
}

}

class CustomButton: UIButton {

private let arrowImageSize: CGSize = CGSize(width: 20, height: 20)
private let sideOffset: CGFloat = 10

override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = .white

    addSubview(attrTitleLabel)
    addSubview(arrowImageView)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

lazy var attrTitleLabel: UILabel = {
    let label = UILabel()
    label.font = UIFont(name: "NoirPro-SemiBold", size: 20)
    label.textColor = .black
    return label
}()

lazy var arrowImageView: UIImageView = {
    let iv = UIImageView()
    iv.image = UIImage(named: "arrow_down")
    iv.contentMode = .scaleAspectFit
    return iv
}()

override func layoutSubviews() {
    super.layoutSubviews()

    arrowImageView.frame = CGRect(x: self.frame.width - arrowImageSize.width - sideOffset,
                                  y: self.frame.height/2 - arrowImageSize.height/2,
                                  width: arrowImageSize.width,
                                  height: arrowImageSize.height)

    attrTitleLabel.frame = CGRect(x: sideOffset, y: 0, width: self.frame.width - sideOffset*2 - arrowImageSize.width, height: self.frame.height)
}

}

外觀:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM