53
49

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIViewの位置操作・取得を簡単に行うための拡張

Last updated at Posted at 2015-06-29

#What is this?

An extension to get/set UIView's position easier.
Need a sample? See just below UIView+Extension.swift.

#これは?
下記投稿をSwiftに書き換えました。
UIViewの位置取得・操作のための便利カテゴリー

このままコピペ→保存で使えます。
使い方は末尾に。

UIView+Extension.swift
import UIKit

extension UIView {

	var top : CGFloat{
		get{
			return self.frame.origin.y
		}
		set{
			var frame		= self.frame
			frame.origin.y	= newValue
			self.frame		= frame
		}
	}

	var bottom : CGFloat{
		get{
			return frame.origin.y + frame.size.height
		}
		set{
			var frame		= self.frame
			frame.origin.y	= newValue - self.frame.size.height
			self.frame		= frame
		}
	}
	
	var right : CGFloat{
		get{
			return self.frame.origin.x + self.frame.size.width
		}
		set{
			var frame		= self.frame
			frame.origin.x	= newValue - self.frame.size.width
			self.frame		= frame
		}
	}

	var left : CGFloat{
		get{
			return self.frame.origin.x
		}
		set{
			var frame		= self.frame
			frame.origin.x	= newValue
			self.frame		= frame		
		}
	}
}

#使い方:


var foo = UIView()
var bar = UIView()
var baz = UILabel()

...

// fooを左から10pxの位置に配置
foo.left	= 10

// barをfooの右から10pxの位置に配置
bar.left	= foo.right + 10

// UIViewを継承しているので、UILabelでも使える
baz.left	= bar.right + 10

便利。
位置関連のコードはダラダラ長くなりがちだしダルいんで、少しでも楽したい。

53
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
49

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?