39
41

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の位置取得・操作のための便利カテゴリー

Posted at

UIViewの位置を操作するとき

CGRect frame = view.frame;
frame.origin.x = 10;
view.frame = frame;

みたいにすると思うのですが、いちいちめんどくさいので、カテゴリーにしてみました。

UIView+Origin.h
@interface UIView (Origin)

@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat right;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat left;

@end
UIView+Origin.m
@implementation UIView (Origin)

- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right
{
    CGRect frame = self.frame;
    frame.origin.x = right - self.frame.size.width;
    self.frame = frame;
}

- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom
{
    CGRect frame = self.frame;
    frame.origin.y = bottom - self.frame.size.height;
    self.frame = frame;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

@end

これを使うと、例えば縦にならぶviewの位置を操作するとき、

#import "UIView+Origin.h"

viewA.top = 10;
viewB.top = viewA.bottom + 10;
viewC.top = viewB.bottom + 10;

なんて書けます。このカテゴリーを使わないとかなりめんどくさいです。

39
41
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
39
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?