OBJC类扩展之尺寸UIView+Size

Posted 山里的和尚会玩水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OBJC类扩展之尺寸UIView+Size相关的知识,希望对你有一定的参考价值。

import

import “UIView+Size.h”


@implementation UIView (Size)

- (void)setSize:(CGSize)size;
{
CGPoint origin = [self frame].origin;

[self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
}

- (CGSize)size;
{
return [self frame].size;
}

- (CGFloat)left;
{
return CGRectGetMinX([self frame]);
}

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

- (CGFloat)top;
{
return CGRectGetMinY([self frame]);
}

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

- (CGFloat)right;
{
return CGRectGetMaxX([self frame]);
}

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

[self setFrame:frame];
}

- (CGFloat)bottom;
{
return CGRectGetMaxY([self frame]);
}

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

[self setFrame:frame];
}

- (CGFloat)centerX;
{
return [self center].x;
}

- (void)setCenterX:(CGFloat)centerX;
{
[self setCenter:CGPointMake(centerX, self.center.y)];
}

- (CGFloat)centerY;
{
return [self center].y;
}

- (void)setCenterY:(CGFloat)centerY;
{
[self setCenter:CGPointMake(self.center.x, centerY)];
}

- (CGFloat)width;
{
return CGRectGetWidth([self frame]);
}

- (void)setWidth:(CGFloat)width;
{
CGRect frame = [self frame];
frame.size.width = width;

[self setFrame:CGRectStandardize(frame)];
}

- (CGFloat)height;
{
return CGRectGetHeight([self frame]);
}

- (void)setHeight:(CGFloat)height;
{
CGRect frame = [self frame];
frame.size.height = height;
[self setFrame:CGRectStandardize(frame)];
}

@end

以上是关于OBJC类扩展之尺寸UIView+Size的主要内容,如果未能解决你的问题,请参考以下文章

绘制两个形状的共同轮廓,每个形状在不同的 UIView - ObjC

扩展 UIView 高度 (Swift)

使用 UIPanGesture 将图像的位置存储到 Objc 中 UIView 中的新位置

从 XIB 加载 UIView 大小不正确

使用 Swift 扩展 UIView 类

轻量级应用开发之(02)UIVIew