ios拓展14-调整button内部控件位置Swift版
2016-08-01 本文已影响207人
Abler
之前笔者写过一个OC版本,http://www.jianshu.com/p/a81a874a33bc
这个是Swift版本, 但是两个版本使用的方法不一样, 效果一样
override init(frame: CGRect) {
super.init(frame: frame)
imageView?.contentMode = .Center// 图片模式
titleLabel?.textAlignment = .Center// 文字木事
titleLabel?.font = UIFont.systemFontOfSize(15)//字体大小
/*=====还有很多其他属性,根据需求自己更改=====*/
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//==========>根据实际需要更改frame
override func layoutSubviews() {
super.layoutSubviews()
// 对子 控件 布局, 左右显示,改为上下显示 (按钮初始化的size为(60,80))
// 设置imageView
// 设置imageView
imageView?.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
// 设置title
titleLabel?.frame = CGRect(x: 0, y: 60, width: 60, height: 20)
+++++++++++++++++++++++++++++++++++++
由于layoutSubviews在会多次调用,CGRect(x: 0, y: 0, width: 60, height: 60)最好使用数字,
如果使用frame设置,在按钮有放大缩小的动画时候, 会有bug
+++++++++++++++++++++++++++++++++++++
}