How to set the title of UIButton as left alignment?

后端 未结 12 2053
感动是毒
感动是毒 2020-11-27 08:34

I need to display the email address from left side of a UIButton, but it is being positioned in the centre.

Is there any way to set the alignment to the

12条回答
  •  猫巷女王i
    2020-11-27 09:30

    In Swift 5.0 and Xcode 10.2

    You have two ways to approaches

    1) Direct approach

    btn.contentHorizontalAlignment = .left
    

    2) SharedClass example (write once and use every ware)

    This is your shared class(like this you access all components properties)

    import UIKit
    
    class SharedClass: NSObject {
    
        static let sharedInstance = SharedClass()
    
        private override init() {
    
        }
    }
    
    //UIButton extension
    extension UIButton {
        func btnProperties() {
            contentHorizontalAlignment = .left
        }
    }
    

    In your ViewController call like this

    button.btnProperties()//This is your button
    

提交回复
热议问题