一个比较简单能满足大部分需求的extensin,如下:
swift 的基于UIView的位置属性
包含:
上下左右:left、right、top、botom
高宽:height、width
中心点:centerX、centerY
直接拷贝作为你的extension文件即可。
/// MARK - UIView
extension UIView { // MARK: - 常用位置属性 public var left:CGFloat {
get {
return self.frame.origin.x
}
set(newLeft) {
var frame = self.frame
frame.origin.x = newLeft
self.frame = frame
}
} public var top:CGFloat {
get {
return self.frame.origin.y
} set(newTop) {
var frame = self.frame
frame.origin.y = newTop
self.frame = frame
}
} public var width:CGFloat {
get {
return self.frame.size.width
} set(newWidth) {
var frame = self.frame
frame.size.width = newWidth
self.frame = frame
}
} public var height:CGFloat {
get {
return self.frame.size.height
} set(newHeight) {
var frame = self.frame
frame.size.height = newHeight
self.frame = frame
}
} public var right:CGFloat {
get {
return self.left + self.width
}
} public var bottom:CGFloat {
get {
return self.top + self.height
}
} public var centerX:CGFloat {
get {
return self.center.x
} set(newCenterX) {
var center = self.center
center.x = newCenterX
self.center = center
}
} public var centerY:CGFloat {
get {
return self.center.y
} set(newCenterY) {
var center = self.center
center.y = newCenterY
self.center = center
}
} }