关于分类创建

  • 系统类的方法不够,不想自定义控件,可用用分类给系统类添加新的方法
  • 常见分类,frame , 写frame太麻烦,需要点几下才能出来x,y,width,height。
    而且 oc对象frame结构体的不能直接进行修改。 需要创建临时变量,这样又很麻烦。所以给 UIView加frame 分类,方便读取修改frame。
  • 可能会说 ,分类怎么会有成员属性,不是不能添加成员属性吗? 其实我们只是想拿到set 和get方法而已。
#import <UIKit/UIKit.h>
@interface UIView (Frame)
// @property在分类里面只会自动生成get,set方法,并不会生成下划线的成员属性
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@end

#import "UIView+Frame.h"
@implementation UIView (Frame)
- (CGFloat)width
{    return self.frame.size.width;}
- (void)setWidth:(CGFloat)width
{    CGRect frame = self.frame;    
frame.size.width = width;   
 self.frame = frame;}
- (CGFloat)heigh
t{    return self.frame.size.height;}
- (void)setHeight:(CGFloat)height
{    CGRect frame = self.frame;  
  frame.size.height = height;    
self.frame = frame;}
- (CGFloat)x
{    return self.frame.origin.x;}
- (void)setX:(CGFloat)x
{    CGRect frame = self.frame;   
 frame.origin.x = x;   
 self.frame = frame;}
- (CGFloat)y
{    return self.frame.origin.y;}
- (void)setY:(CGFloat)y
{    CGRect frame = self.frame;   
 frame.origin.y = y;   
 self.frame = frame;}
@end
  • 还有一个分类就是 图片的分类,对于ios7以后,由于扁平化管理,所以对于其navigationBar上面的按钮(图片 以及 字体)统一渲染成了蓝色, 所以需要进行修改,对于字体的话可以直接修改。
    对于图片的话,需要重新改其渲染方式,让其不进行渲染就行了
#import <UIKit/UIKit.h>
@interface UIImage (Image)
// 返回一张没有渲染的图片
+ (instancetype)imageWithOriginRenderingName:(NSString *)imageName;
@end

#import "UIImage+Image.h"
@implementation UIImage (Image)

+ (instancetype)imageWithOriginRenderingName:(NSString *)imageName
{    UIImage *image = [UIImage imageNamed:imageName];       
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,403评论 25 708
  • 走在金黄色的路上,正如踏着我的少年时代,渐渐远去,我不知将去何方。“每一个不曾起舞的日子都是对生命的辜负”我也不知...
    小狸花阅读 303评论 0 1
  • 序:为何不能用英语来输出倒逼输入,而这本身又是一次输出倒逼输入。 注:由于是首篇我就不用中文再翻译了,一者学识较浅...
    滕公子实习录阅读 371评论 0 0
  • 给一个对象设置属性并不仅仅是添加一个新属性或者修改已有的属性值,今天完整的讲讲这个过程 myObject.name...
    peppermint_egg阅读 2,665评论 2 0