zoukankan      html  css  js  c++  java
  • UIApearance的认识

    在参加工作之前一直不知道还有UIApearance的这个属性,并且不知道UIApearance是用来干嘛的,还不知道怎么用,工作之后,看公司代码中都会出现这个UIApearance,我决定学习学习,并掌握好UIApearance的基本知识和掌握其用法.

    在iOS 5以前,我们想去自定义系统控件的外观是一件麻烦的事。如果想统一地改变系统控件的外观,我们可能会想各种办法,如去继承现有的控件类,并在子类中修改,或者甚至于动用method swizzling这样高大上的方法。不过,苹果在iOS 5之后为我们提供了一种新的方法:UIAppearance,让这些事简单了不少。在这里,我们就来总结一下吧。

     

    一.作用:

    UIApearance实际上是一个协议,我们可以用它来获取一个类的外观代理(appearance proxy)。为什么说是一个类,而不明确说是一个视图或控件呢?这是因为有些非视图对象(如UIBarButtonItem)也可以实现这个协议,来定义其所包含的视图对象的外观。我们可以给这个类的外观代理发送一个修改消息,来自定义一个类的实例的外观。

    我们以系统定义的控件UIButton为例,根据我们的使用方式,可以通过UIAppearance修改整个应用程序中所有UIButton的外观,也可以修改某一特定容器类中所有UIButton的外观(如UIBarButtonItem)。不过需要注意的是,这种修改只会影响到那些执行UIAppearance操作之后添加到我们的视图层级架构中的视图或控件,而不会影响到修改之前就已经添加的对象。因此,如果要修改特定的视图,先确保该视图在使用UIAppearance后才通过addSubview添加到视图层级架构中。

     

    二.使用:

    例如,如果我们想修改UINavigationBar的所有实例的背影颜色和标题外观:

    UINavigationBar.appearance().barTintColor = UIColor(red: 104.0/255.0, green: 224.0/255.0, blue: 231.0/255.0, alpha: 1.0)
     
    UINavigationBar.appearance().titleTextAttributes = [
        NSFontAttributeName: UIFont.systemFontOfSize(15.0),
        NSForegroundColorAttributeName: UIColor.whiteColor()
    ]

    我们也可以指定一类容器,在这个容器中,我们可以自定义一个类的所有实例的外观。我们可以使用下面这些方法:

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
       setBackgroundImage:myNavBarButtonBackgroundImage forState:state barMetrics:metrics];
     
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil]
        setBackgroundImage:myPopoverNavBarButtonBackgroundImage forState:state barMetrics:metrics];
     
    [[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil]
        setBackgroundImage:myToolbarButtonBackgroundImage forState:state barMetrics:metrics];
     
    [[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], [UIPopoverController class], nil]
        setBackgroundImage:myPopoverToolbarButtonBackgroundImage forState:state barMetrics:metrics];
  • 相关阅读:
    【Redis】事务
    【Web】Apache HttpClient & HttpAsyncClient
    【Spring】导入配置文件
    【SpringBoot】@Conditional注解家族
    【前端开发】dva+antd+react
    【Spring】EnableXXX
    POJ-2240-Arbitrage
    POJ-2387-Til the Cows Come Home
    hdu-1847-畅桶工程续
    Floyd算法模板(多源最短)
  • 原文地址:https://www.cnblogs.com/pengsi/p/5587366.html
Copyright © 2011-2022 走看看