Add Swift File:uiview+extension
Import uikitextension UIView {//xvar x:cgfloat {Get { returnframe.origin.x}Set(newval) {var tmpframe:cgrect=Frame tmpframe.origin.x=newval Frame=Tmpframe}} //yvar y:cgfloat {Get { returnFRAME.ORIGIN.Y}Set(newval) {var tmpframe:cgrect=Frame TMPFRAME.ORIGIN.Y=newval Frame=Tmpframe}} //Heightvar height:cgfloat {Get { returnFrame.size.height}Set(newval) {var tmpframe:cgrect=Frame TmpFrame.size.height=newval Frame=Tmpframe}} //widthvar width:cgfloat {Get { returnFrame.size.width}Set(newval) {var tmpframe:cgrect=Frame TmpFrame.size.width=newval Frame=Tmpframe}} // Leftvar left:cgfloat {Get { returnx}Set(newval) {x=newval}} // Rightvar right:cgfloat {Get { returnX +width}Set(newval) {x= newval-width}} //Topvar top:cgfloat {Get { returny}Set(newval) {y=newval}} //Bottomvar bottom:cgfloat {Get { returnY +Height}Set(newval) {y= newval-Height}} var centerx:cgfloat {Get { returncenter.x}Set(newval) {Center=Cgpoint (X:newval, Y:center.y)}} var centery:cgfloat {Get { returnCenter.y}Set(newval) {Center=Cgpoint (x:center.x, Y:newval)}} var middlex:cgfloat {Get { returnWidth/2}} var middley:cgfloat {Get { returnHeight/2}} var middlepoint:cgpoint {Get { returnCgpoint (X:middlex, Y:middley) }}}
Simple Setup X/y/width/height
Import uikitextension UIView {//set a random background colorfunc backgroundcolorrandom () {Self.backgroundcolor= Uicolor (Red:cgfloat (CGFloat (Random ())/cgfloat (Rand_max)), Green:cgfloat (CGFloat (Random ())/cgfloat (Rand_max)), Blue:cgfloat (CGFloat (Random ())/cgfloat (Rand_max)), Alpha:1)} var x:cgfloat!{ Get{NSLog ("Get") returnself.frame.origin.x}Set(newx) {var rect:cgrect! =self.frame rect.origin.x=newx Self.frame=rect NSLog ("Set")}} var y:cgfloat!{ Get{ returnSELF.FRAME.ORIGIN.Y}Set(newy) {var rect:cgrect! =self.frame RECT.ORIGIN.Y=Newy Self.frame=rect}} var width:cgfloat!{ Get{ returnSelf.frame.size.width}Set(NEWW) {var rect:cgrect! =self.frame rect.size.width=NEWW Self.frame=rect}} var height:cgfloat!{ Get{ returnSelf.frame.size.height}Set(NEWH) {var rect:cgrect! =self.frame rect.size.height=NEWH Self.frame=rect}} }
RELATED links:Swift about UIView setting frame value extension
Swift adds extension extension frame to UIView