1. 程式人生 > >Swift裡自定義一個文字在左、圖片在右的,標題按鈕

Swift裡自定義一個文字在左、圖片在右的,標題按鈕

//
//  TitleButton.swift


import UIKit

class TitleButton: UIButton {

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

        setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal
) setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Highlighted) sizeToFit() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } /** 這個方法裡調整佈局 */ override func layoutSubviews() { super.layoutSubviews
() titleLabel?.frame.origin.x = 0 imageView?.frame.origin.x = titleLabel!.frame.size.width } }

2.使用

 let titleBtn = TitleButton()