UIView扩展--方便控件的布局

iOS——UI基础知识 专栏收录该内容
2 篇文章 0 订阅

通过这个扩展类,可以比较方便地用代码来控制UI布局。

.h文件


#import <UIKit/UIKit.h>

@interface UIView (Layout)

@property (nonatomic, assign) CGFloat originalX;//视图左上角的X值
@property (nonatomic, assign) CGFloat originalY;//视图左上角的Y值
@property (nonatomic, assign, readonly) CGFloat maxX;//视图右下角的X值
@property (nonatomic, assign, readonly) CGFloat maxY;//视图右下角的Y值
@property (nonatomic, assign) CGFloat width;//视图的宽
@property (nonatomic, assign) CGFloat height;//视图的高
@property (nonatomic, assign) CGSize viewSize;//视图的宽和高
@property (nonatomic, assign) CGFloat centerPointX;//中心位置的X坐标
@property (nonatomic, assign) CGFloat centerPointY;//中心位置的Y坐标

@end

.m文件


#import "UIView+Layout.h"

@implementation UIView (Layout)

//setting Methods
- (void)setOriginalX:(CGFloat)originalX
{
    CGRect frame = self.frame;
    frame.origin.x = originalX;
    self.frame = frame;
}

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


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


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


- (void)setViewSize:(CGSize)viewSize
{
    CGRect frame = self.frame;
    frame.size = viewSize;
    self.frame = frame;
}

- (void)setCenterPointX:(CGFloat)centerPointX
{
    self.center = CGPointMake(centerPointX, self.center.y);
}

- (void)setCenterPointY:(CGFloat)centerPointY
{
    self.center = CGPointMake(self.center.x, centerPointY);
}


//getting Methods

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

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

- (CGFloat)maxX
{
    return CGRectGetMaxX(self.frame);
}

- (CGFloat)maxY
{
    return CGRectGetMaxY(self.frame);
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGSize)viewSize
{
    return self.bounds.size;
}

- (CGFloat)centerPointX
{
    return self.center.x;
}

- (CGFloat)centerPointY
{
    return self.center.y;
}


@end

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值