iOS给UIView添加常用的Extension

这个看个人习惯,不一定每个人的都一样。最好自己写一个,以后就用自己的。

#import <UIKit/UIKit.h>

@interface UIView (SetSize)

@property (nonatomic) CGFloat frameX;// ①x
@property (nonatomic) CGFloat frameY;// ②y
@property (nonatomic) CGFloat frameWidth;// width
@property (nonatomic) CGFloat frameHeight;// height
@property (nonatomic) CGFloat frameX_Width;// x + width(没有set方法)
@property (nonatomic) CGFloat frameY_Height;// y + height(没有set方法)

@property (nonatomic) CGFloat distance_right;// ③相对于父视图,距离右侧的距离
@property (nonatomic) CGFloat distance_bottom;// ④相对于父视图,距离底部的距离

@property (nonatomic) CGFloat centerX;// 中心点x
@property (nonatomic) CGFloat centerY;// 中心点y

@property (nonatomic) CGPoint origin;// origin
@property (nonatomic) CGSize size;// size

/**
 *  相对于①的set方法,同样是设置了x,但是这个方法的width也变了,而距离右侧的距离不变
 */
- (void)setFrameXWithRightUnchanged:(CGFloat)frameX;
/**
 *  相对于②的set方法,同样是设置了y,但是这个方法的height也变了,而距离底部的距离不变
 */
- (void)setFrameYWithBottomUnchanged:(CGFloat)frameY;
/**
 *  相对于③的set方法,同样是设置了距离右侧的距离,但是这个方法的width也变了,而x不变
 */
- (void)setDistance_rightWithFrameXUnchanged:(CGFloat)distance_right;
/**
 *  相对于④的set方法,同样是设置了距离底部的距离,但是这个方法的height也变了,而y不变
 */
- (void)setDistance_bottomWithFrameYUnchanged:(CGFloat)distance_bottom;

@end

#import "UIView+SetSize.h"

@implementation UIView (SetSize)

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



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



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



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



- (void)setFrameX_Width:(CGFloat)frameX_Width {
    
}
- (CGFloat)frameX_Width {
    return self.frame.origin.x + self.frame.size.width;
}



- (void)setFrameY_Height:(CGFloat)frameY_Height {
    
}
- (CGFloat)frameY_Height {
    return self.frame.origin.y + self.frame.size.height;
}



- (void)setDistance_right:(CGFloat)distance_right {
    CGRect frame = self.frame;
    frame.origin.x = self.superview.frame.size.width - self.frame.size.width - distance_right;
    self.frame = frame;
}
- (CGFloat)distance_right {
    return self.superview.frame.size.width - self.frame.origin.x - self.frame.size.width;
}



- (void)setDistance_bottom:(CGFloat)distance_bottom {
    CGRect frame = self.frame;
    frame.origin.y = self.superview.frame.size.height - self.frame.size.height - distance_bottom;
    self.frame = frame;
}
- (CGFloat)distance_bottom {
    return self.superview.frame.size.height - self.frame.origin.y - self.frame.size.height;
}



- (void)setCenterX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}
- (CGFloat)centerX {
    return self.center.x;
}



- (void)setCenterY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}
- (CGFloat)centerY {
    return self.center.y;
}



- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}
- (CGPoint)origin {
    return self.frame.origin;
}



- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
- (CGSize)size {
    return self.frame.size;
}



- (void)setFrameXWithRightUnchanged:(CGFloat)frameX {
    CGRect frame = self.frame;
    frame.size.width -= frameX - frame.origin.x;
    frame.origin.x = frameX;
    self.frame = frame;
}
- (void)setFrameYWithBottomUnchanged:(CGFloat)frameY {
    CGRect frame = self.frame;
    frame.size.height -= frameY - frame.origin.y;
    frame.origin.y = frameY;
    self.frame = frame;
}
- (void)setDistance_rightWithFrameXUnchanged:(CGFloat)distance_right {
    CGRect frame = self.frame;
    frame.size.width -= distance_right - self.distance_right;
    self.frame = frame;
}
- (void)setDistance_bottomWithFrameYUnchanged:(CGFloat)distance_bottom {
    CGRect frame = self.frame;
    frame.size.height -= distance_bottom - self.distance_bottom;
    self.frame = frame;
}
@end


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值