美文网首页iOS
获取控件的 frame 相关属性 - iOS

获取控件的 frame 相关属性 - iOS

作者: survivorsfyh | 来源:发表于2019-12-05 11:19 被阅读0次

    日常开发中经常会对相关控件的 frame 进行设置,除了 autolayout 那种以外,人为控制调试尽可能省去各种 CGRectGetXXX(<#CGRect rect#>) ,以下类便是一个便于获取调试的封装类,引入到对应的页面或者设置在全局宏类中即可,哪里需要点哪里 :)

    GitHub

    #import <UIKit/UIKit.h>
    
    @interface UIView (Frame)
    
    @property (nonatomic, assign) CGFloat x;
    @property (nonatomic, assign) CGFloat y;
    @property (nonatomic, assign) CGFloat centerX;
    @property (nonatomic, assign) CGFloat centerY;
    @property (nonatomic, assign) CGFloat width;
    @property (nonatomic, assign) CGFloat height;
    @property (nonatomic, assign) CGSize size;
    @property (nonatomic, assign) CGPoint origin;
    
    @end
    
    #import "UIView+Frame.h"
    
    @implementation UIView (Frame)
    
    - (void)setX:(CGFloat)x
    {
        CGRect frame = self.frame;
        frame.origin.x = x;
        self.frame = frame;
    }
    
    - (void)setY:(CGFloat)y
    {
        CGRect frame = self.frame;
        frame.origin.y = y;
        self.frame = frame;
    }
    
    - (CGFloat)x
    {
        return self.frame.origin.x;
    }
    
    - (CGFloat)y
    {
        return self.frame.origin.y;
    }
    
    - (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)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;
    }
    
    - (CGFloat)height
    {
        return self.frame.size.height;
    }
    
    - (CGFloat)width
    {
        return self.frame.size.width;
    }
    
    - (void)setSize:(CGSize)size
    {
        CGRect frame = self.frame;
        frame.size = size;
        self.frame = frame;
    }
    
    - (CGSize)size
    {
        return self.frame.size;
    }
    
    - (void)setOrigin:(CGPoint)origin
    {
        CGRect frame = self.frame;
        frame.origin = origin;
        self.frame = frame;
    }
    
    - (CGPoint)origin
    {
        return self.frame.origin;
    }
    
    @end
    

    以上便是此次分享的全部内容,希望能对大家有所帮助!

    相关文章

      网友评论

        本文标题:获取控件的 frame 相关属性 - iOS

        本文链接:https://www.haomeiwen.com/subject/fghdgctx.html