简体   繁体   中英

UIBarButton Rendering Issues

Currently, I am implementing somewhat of a custom UIBarButtonItem when I use a couple custom methods to change the positioning of the button image to move it to the right of the text and add some padding. However, when I leave the screen and come back the buttons text and images get switched and distorted as can be seen in the attached images.

在此处输入图片说明

在此处输入图片说明

First picture is the way the button looks when i leave the screen and come back second picture is what it originally looks like. i have included the code for the uibarbuttonitem if anyone sees anything I don't that is causing this rendering issue I would greatly appreciate it.

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 should be of type UIView . I suspect using the higher type of UIButton is causing issues. Within your customView you will need to set up the title and image as sub views and constrain according to your wishes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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