iOS之常用分类frame、button、

*******frame
#import <UIKit/UIKit.h>

@interface UIView (FLExtension)

@property (nonatomic) CGFloat originX;
@property (nonatomic) CGFloat originY;
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat right;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;
@property (nonatomic) CGFloat centerX;
@property (nonatomic) CGFloat centerY;
@property (nonatomic) CGPoint origin;
@property (nonatomic) CGSize  size;

@end

***
#import "UIView+FLExtension.h"

@implementation UIView (FLExtension)


- (void)setOriginX:(CGFloat)originX {
    CGRect frame = self.frame;
    frame.origin.x = originX;
    self.frame =frame;
}

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

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


- (CGFloat)originY {
    return self.frame.origin.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)width
{
    return self.frame.size.width;
    
}

-(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;
    
}

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


-(CGPoint)origin
{
    return self.frame.origin;
    
}

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

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

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

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

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

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

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

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

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

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

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

- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}
@end


 

 

****BUtton


#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIButton (Hximbasebtn)
+(UIButton *)createBtnWithTitle:(NSString *)title image:(NSString *)imageStr frame:(CGRect) frame target:(id)target method:(SEL)method;
@end

NS_ASSUME_NONNULL_END

***
#import "UIButton+Hximbasebtn.h"

@implementation UIButton (Hximbasebtn)
+(UIButton *)createBtnWithTitle:(NSString *)title image:(NSString *)imageStr frame:(CGRect) frame target:(id)target method:(SEL)method{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}
@end

 

*****导航按钮返回和右边按钮

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIBarButtonItem (LeftBarbuttonItem)
+(NSArray *)backBtn:(SEL)action target:(id)target image:(NSString *)imageStr title:(NSString *)titleStr;//返回按钮
+(NSArray *)addrightBtn:(SEL)action target:(id)target image:(NSString *)imageStr title:(NSString *)titleStr;//导航右边按钮
@end

NS_ASSUME_NONNULL_END

***
//导航上按钮

#import "UIBarButtonItem+LeftBarbuttonItem.h"

@implementation UIBarButtonItem (LeftBarbuttonItem)
//添加导航返回按钮

+(NSArray *)backBtn:(SEL)action target:(id)target image:(NSString *)imageStr title:(NSString *)titleStr{
    
    UIButton *backBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,80,44)];
    
    [backBtn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    
    backBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    
    UIBarButtonItem *backbtn=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
    UIBarButtonItem *fixBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    
    
    
    if(titleStr && ![titleStr isEqualToString:@""]){
        
        [backBtn setTitle:titleStr forState:UIControlStateNormal];
        
    }
    
    
    
    if(imageStr && ![imageStr isEqualToString:@""]){
        
        fixBtn.width=-30;
        
        [backBtn setImage:[UIImage imageNamed:imageStr]forState:UIControlStateNormal];
        
    }
    
    if(target && action){
        
        [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    //兼容IOS11
    
    CGFloat sysv= [[[UIDevice currentDevice]systemVersion]floatValue];
    
    if(sysv>=11.0){
        
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        
        [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
        
        
        
    }
    
    
    
    NSArray *btnArr=@[fixBtn,backbtn];
    
    return btnArr;
    
}





//添加导航右侧按钮

+(NSArray *)addrightBtn:(SEL)action target:(id)target image:(NSString *)imageStr title:(NSString *)titleStr{
    
    UIButton *rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,0,80,44)];
    
    [rightBtn setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    
    [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:17 weight:UIFontWeightThin]];
    
    UIBarButtonItem *rbtn=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    UIBarButtonItem *fixBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    
    
    if(titleStr && ![titleStr isEqualToString:@""]){
        
        [rightBtn setTitle:titleStr forState:UIControlStateNormal];
        
        fixBtn.width=0;
        
    }
    
    
    
    if(imageStr && ![imageStr isEqualToString:@""]){
        
        [rightBtn setImage:[UIImage imageNamed:imageStr]forState:UIControlStateNormal];
        
        fixBtn.width=-20;
        
    }
    
    if(target && action){
        
        [rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    //兼容IOS11
    
    CGFloat sysv= [[[UIDevice currentDevice]systemVersion]floatValue];
    
    if(sysv>=11.0){
        
        rightBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
        
        [rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,0)];
        
    }
    
    NSArray *rightBtnArr=@[fixBtn,rbtn];
    
    return rightBtnArr;
    
}


@end

 

发布了696 篇原创文章 · 获赞 109 · 访问量 96万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览