简体   繁体   English

UIButton中的按钮和图像对齐问题

[英]Button and Image Alignment issues in UIButton

So I have a UIBarbuttonItem that I am currently designing based off of a layout that I have done. 所以我有一个基于当前布局设计的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")
    }

}

I correctly do the job of using both an image and title and setting the image insets for the button and on load the appearance is great. 我正确地使用了图像和标题,并为按钮设置了图像插图,并且在加载时外观很好。 However, when I leave the screen and come back it seems as though everything is thrown out of wack the image gets moved back and sometimes there will be two images and one will have a distorted size. 但是,当我离开屏幕并返回时,似乎一切都乱丢了,图像又被移回了,有时会有两张图像,其中一张的尺寸会变形。

Is there anything wrong with my custom button implementation that I am missing. 我缺少的自定义按钮实现有什么问题吗?

I have included images for before and after 我包含了之前和之后的图像 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

I suggest you to make your custom button class, then make title and image by adding subviews. 我建议您创建自定义按钮类,然后通过添加子视图来创建标题和图像。 In this case UIImageView and UILabel. 在这种情况下,UIImageView和UILabel。 Because UIButton inherits from UIView you can easy do this. 因为UIButton继承自UIView,所以您可以轻松做到这一点。 I've never had problems using this way. 使用这种方式我从来没有遇到过问题。

Here is the code I've written for you: 这是我为您编写的代码:

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)
}

}

How it looks: 外观:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM