如果有帮助到你就给个赞吧 谢谢咯...
.h
#import <UIKit/UIKit.h>
@interface UIView (Extension)
/**
控件左上角 x 坐标
*/
@property (nonatomic, assign) CGFloat x;
/**
控件左上角 y 坐标
*/
@property (nonatomic, assign) CGFloat y;
/**
控件的中心点 x 坐标
*/
@property (nonatomic, assign) CGFloat centerX;
/**
控件的中心点 y 坐标
*/
@property (nonatomic, assign) CGFloat centerY;
/**
控件的宽度
*/
@property (nonatomic, assign) CGFloat width;
/**
控件高度
*/
@property (nonatomic, assign) CGFloat height;
/**
控件size
*/
@property (nonatomic, assign) CGSize size;
/**
* 判断一个控件是否真正显示在主窗口
*/
- (BOOL)isShowingOnKeyWindow;
/**
加载xib 创建的 View
*/
+ (instancetype)viewFromXib;
.m
#import "UIView+Extension.h"
@implementation UIView (Extension)
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (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)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)size
{
return self.frame.size;
}
/*
* 当前这个View是否和keyWindow有重叠
*/
- (BOOL)isShowingOnKeyWindow
{
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
CGRect winBounds = keyWindow.bounds;
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}
+ (instancetype)viewFromXib
{
NSString *className = NSStringFromClass([self class]);
id object = [[[NSBundle mainBundle]loadNibNamed:className owner:nil options:nil]lastObject];
if ([object isKindOfClass:[self class]]) {
return object;
}
return nil;
}
UIViewController 分类
.h 文件
#import <UIKit/UIKit.h>
@interface UIViewController (Extension)
/**
从xib中加载控制器
*/
+ (instancetype)viewControllerFromNib;
/**
从 storyboard 加载ViewController
@param storyboardName storyboard 名字
@param identifier 这个类的 storyboard ID(自己设置)
*/
+ (instancetype)viewControllerFromStoryboardName:(NSString *)storyboardName Identifier:(NSString *)identifier;
@end
.m 文件
#import "UIViewController+Extension.h"
@implementation UIViewController (Extension)
+ (instancetype)viewControllerFromNib {
UIViewController *viewController = [[self alloc] initWithNibName:NSStringFromClass([self class]) bundle:nil];
return viewController;
}
+ (instancetype)viewControllerFromStoryboardName:(NSString *)storyboardName Identifier:(NSString *)identifier{
return [[UIStoryboard storyboardWithName:storyboardName bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:identifier];
}
@end
干货代码 我的一个纯代码的基础框架有各种常用的分类封装入手即用