//
// TitleButton.swift
// weibo
//
// Created by mac on 17/8/10.
// Copyright 2017年 xww All rights reserved.
//
import UIKit
class TitleButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
//设置按钮图片
self.setImage(#imageLiteral(resourceName: "navigationbar_arrow_down"), for: UIControlState.normal)
setImage(#imageLiteral(resourceName: "navigationbar_arrow_up"), for: .selected)
//设置标题颜色
setTitleColor(UIColor.darkGray, for: UIControlState.normal)
setTitleColor(UIColor.blue, for: UIControlState.highlighted)
//设置按钮背景颜色
backgroundColor = UIColor.magenta
//设置按钮标题水平或垂直位置
contentHorizontalAlignment = UIControlContentHorizontalAlignment.right
contentVerticalAlignment = UIControlContentVerticalAlignment.center
//设置按钮标题大小
titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
//设置标题的原点
titleLabel?.frame.origin.x = 0
//设置图片的原点
imageView?.frame.origin.x = titleLabel!.bounds.width
//设置文字与图片间距
titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
//设置图片与文字间距
imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
//设置按钮中点在父视图的坐标
center = CGPoint.init(x: 75, y: 150)
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!.bounds.width//设置图片的原点
//
// }
}
iOS swift3.0之自定义按钮button
最新推荐文章于 2022-12-08 17:47:10 发布