美文网首页iOS Dev
UIAppearance学习笔记

UIAppearance学习笔记

作者: 稻草人的小秘密 | 来源:发表于2016-02-15 15:52 被阅读884次

@(iOS & Objective-C & iOS架构)

做App的时候,我们经常需要自定义大量的界面,来让我们的界面看起来更加的美观。自从iOS5之后,UIAppearance协议的出现让我们的可以快速的修改系统控件的UI。

note
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
当一个view进入window的时候才可以实现外观的变化,当一个view已经进入window的时候再进行设置是不会做变更的,对于已经存在window中的view做外观设置需要从当前的视图层级中移除并重新加载。

想要使用这个协议来自定义UI,类必须遵从UIAppearanceContainer协议,并且相关的属性需要有UI_APPEARANCE_SELECTOR标记。


自己创建可自定义外观的控件

@interface CustomCell : UITableViewCell
- (void)setSelectedBackgroundColor:(UIColor*)color UI_APPEARANCE_SELECTOR;
@end

@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView = [UIView new];
        self.selectedBackgroundView.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}
- (void)setSelectedBackgroundColor:(UIColor*)color{
    self.selectedBackgroundView.backgroundColor = color;
}
@end

UIAppearance实现原理

在通过UIAppearance调用“UI_APPEARANCE_SELECTOR”标记的方法来配置外观时,UIAppearance实际上没有进行任何实际调用,而是把这个调用保存起来(在Objc中可以用NSInvocation对象来保存一个调用)。当实际的对象显示之前(添加到窗口上,drawRect:之前),就会对这个对象调用之前保存的调用。当这个setter调用后,你的界面风格自定义就完成了。

参考:http://blog.xcodev.com/archives/custom-ui-using-uiappearance/

相关文章

  • UIAppearance学习笔记

    @(iOS & Objective-C & iOS架构) 做App的时候,我们经常需要自定义大量的界面,来让我们的...

  • UIAppearance

    今天想聊的是UIKit: UIAppearance UIAppearance是什么? UIAppearance实际...

  • UIAppearance的学习

    介绍 UIApearance是一个协议,我们可以通过它来获取一个类的外观代理。通过给这个类的外观代理发送修改消息来...

  • App主题

    UIAppearance Tutorial

  • UIAppearance学习和使用

    我们可以通过UIAppearance协议的方法来给整个项目中某一类控件添加全局样式,或者项目中某个类的子类控件添加...

  • 主题方案

    主题方案 方案一:通过原生接口UIAppearance(废弃) 凡是遵循UIAppearance协议的控件都可以在...

  • ios注意问题

    1.使用UIAppearance注意的问题 如果不熟悉可以点击了解, UIAppearance它的目的就是设置全局...

  • Appearance

    UIAppearance是一个协议 UIView默认已经遵守了这个协议 来看看UIAppearance都有什么方法...

  • UIAppearance

    一、UIApplication 1.简单介绍 1). UIApplication对象是应用程序的象征,一个UIAp...

  • UIAppearance

    1,系统控件的使用及注意实现 注意:1,+appearance 在设置之后的控件才起作用,这就是为什么在控制器中...

网友评论

    本文标题:UIAppearance学习笔记

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