Tuesday, October 6, 2026
HomeiOS DevelopmentEasy methods to set picture to proper edge nook in UIButton in/after...

Easy methods to set picture to proper edge nook in UIButton in/after iOS 15? (Swift)

[ad_1]

I am going through a problem in iOS 15 the place when setting the picture to the fitting nook does not work whereas earlier than iOS 15 it’s working correctly.
What I wish to obtain:
ExpectedResult

What I am reaching:
ActualResult

Right here is the entire code which I am utilizing for iOS 15:

import UIKit

class ViewController: UIViewController { 
 
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        let btnDropdown = getDropdownButton()
        btnDropdown.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(btnDropdown)
        NSLayoutConstraint.activate([
            btnDropdown.heightAnchor.constraint(equalToConstant: 44),
            btnDropdown.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
            btnDropdown.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
            btnDropdown.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 30)
        ])
    }
    
    fileprivate func getDropdownButton() -> UIButton {
        var button: UIButton!
        if #out there(iOS 15.0, *),
           let btnConfig = getButtonConfiguration() {
            button = UIButton(configuration: btnConfig)
        } else {
            button = UIButton(kind: .customized)
        }
        button.setTitle("Choose Tags", for: .regular)
        setupGeneralSettings(for: button)
        return button
    }
    
    fileprivate func setupGeneralSettings(for button: UIButton) {
        button.titleEdgeInsets = UIEdgeInsets(prime: 0, left: 8, backside: 0, proper: 8)
        button.contentEdgeInsets = UIEdgeInsets(prime: 0, left: 8, backside: 0, proper: 8)
        button.contentHorizontalAlignment = .left
        button.layer.borderWidth = 1
        button.layer.cornerRadius = 8
        button.clipsToBounds = true
        button.backgroundColor = .lightGray.withAlphaComponent(0.3)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        button.setTitleColor(UIColor.black, for: .regular)
    }
    
    @out there(iOS 15.0, *)
    fileprivate func getButtonConfiguration() -> UIButton.Configuration? {
        var btnConfig = UIButton.Configuration.plain()
        btnConfig.buttonSize = .medium
        btnConfig.titleAlignment = .main
        btnConfig.imagePlacement = .trailing
        btnConfig.picture = UIImage(systemName: "arrowtriangle.down.circle")
        btnConfig.contentInsets = NSDirectionalEdgeInsets(prime: 0, main: 8, backside: 0, trailing: 8)
        btnConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
            var outgoing = incoming
            outgoing.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            return outgoing
        }
        return btnConfig
    }
}

NOTE: After I say iOS 15, I imply that I am utilizing UIButton.Configuration as a result of because the UIButton’s titleEdgeInset, imageEdgeInset & contentEdgeInset is deprecated in iOS 15.
Additionally, I believe there’s something I am lacking in UIButton.Configuration by which the picture will get to the fitting fringe of the button however I am unsure what it’s.

Please let me know if there may be some other data that is wanted.
Thanks in Advance!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments