swift自定义btn

xiaoxiao2021-02-28  113

自定义btn,文字在左图片在右的 效果。

//

//  TitleBtn.swift

//  FlowerField

//

//  Created by CJW on 17/6/7.

//  Copyright © 2017 cjw. All rights reserved.

//

import UIKit

class TitleBtn: UIButton {

    override init(frame: CGRect) {

        super.init(frame: frame)

        setTitle("主题", forState: .Normal)

        setImage(UIImage(named: "hp_arrow_down"), forState: .Normal)

        setImage(UIImage(named: "hp_arrow_up"), forState: .Selected)

        setTitleColor(UIColor.blackColor(), forState: .Normal)

        titleLabel?.font = UIFont.systemFontOfSize(15)

        sizeToFit()

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    override func layoutSubviews() {

        if imageView?.frame.origin.x < titleLabel?.frame.origin.x{

            titleLabel?.frame.origin.x = imageView!.frame.origin.x

            imageView?.frame.origin.x = CGRectGetMaxX((titleLabel?.frame)!) + 10

        }

        

    }

}

转载请注明原文地址: https://www.6miu.com/read-38169.html

最新回复(0)