自定义其他控件类方法也类似
import UIKit
let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
class MyView: UIView {
//1 声明变量
var myImageView: UIImageView?
override init(frame: CGRect) {
super.init(frame: frame)
//2 初始化视图
myImageView = UIImageView(frame: CGRect(x: SCREEN_WIDTH/2 - 87.5, y: SCREEN_HEIGHT/2 - 180, width: 175, height: 256))
self.addSubview(myImageView!)
}
//3 增加设置图片方法
func addImage(image: UIImage) {
myImageView?.image = image
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
使用
let yourView = MyView() //按照图纸要求征块地
yourView.addImage(UIImage(named: "imageName")!) //设置图片