Skip to content

Instantly share code, notes, and snippets.

@shar0
Created July 2, 2018 09:50
Show Gist options
  • Save shar0/9f0e8eadd02fb15d591401d1c5d12d03 to your computer and use it in GitHub Desktop.
Save shar0/9f0e8eadd02fb15d591401d1c5d12d03 to your computer and use it in GitHub Desktop.
I'm tired to write more frame.origin.x/y or frame.size.width/height.
//
// ViewExtension.swift
// Rina
//
// Created by Joe Wang on 02/07/2018.
// License: WTFPL
//
import UIKit
extension UIView {
var x:CGFloat {
get {
return frame.origin.x
}
set {
frame = CGRect(x: newValue, y: y, width: width, height: height)
}
}
var y:CGFloat {
get {
return frame.origin.y
}
set {
frame = CGRect(x: x, y: newValue, width: width, height: height)
}
}
var width:CGFloat {
get {
return frame.size.width
}
set {
frame = CGRect(x: x, y: y, width: newValue, height: height)
}
}
var height:CGFloat {
get {
return frame.size.height
}
set {
frame = CGRect(x: x, y: y, width: width, height: newValue)
}
}
func getRelativeX(_ offset:CGFloat = 0 ) -> CGFloat {
return frame.origin.x + frame.width + offset
}
func getRelativeY(_ offset:CGFloat = 0 ) -> CGFloat {
return frame.origin.y + frame.height + offset
}
func getCenterX(_ width:CGFloat, _ offset:CGFloat = 0 ) -> CGFloat {
return (width - frame.width) / 2 + offset
}
func getCenterY(_ height:CGFloat, _ offset:CGFloat = 0 ) -> CGFloat {
return (height - frame.height) / 2 + offset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment