【问题标题】:conditional global format to UIButton in iOSiOS中UIButton的条件全局格式
【发布时间】:2013-10-01 17:20:49
【问题描述】:

我正在开发一个应用程序并应用全局格式,为此我在我的 appDelegate.m 文件中添加了以下几行

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    [self customizeAppearance];

    return YES;
}

-(void)customizeAppearance{

    int iOSVersion = [[[UIDevice currentDevice]systemVersion] intValue];

    if (iOSVersion >= 7) {

        //Customizing UIButton
        [[UIButton appearance]setBackgroundColor:(UIColorFromRGB(toolbarTintColor))];
        [[UIButton appearance]setTitleColor:(UIColorFromRGB(toolbarTextColor)) forState:UIControlStateNormal];

      }

    if (iOSVersion < 7) {

        //Customizing UIButton
        [[UIButton appearance]setBackgroundImage:[UIImage imageNamed:@"Button.png"] forState:UIControlStateNormal];
    }
}

我根据 iOS 版本做了这两种样式,这会改变 UIbutton 样式,但我希望它只应用于 UIButtons,而不是 UIBarButtonItems 这可能吗??

更进一步,我想将此格式全局应用于 UIView 元素内的 UIButtons(不在 UITableViewCells 左右)可以这样做吗???

提前感谢您的支持

【问题讨论】:

    标签: ios objective-c uibutton styles


    【解决方案1】:

    您执行此操作的方式应该按照您所描述的方式进行。 UIBarButtonItem 并不像名称所暗示的那样从 UIButton 继承。这意味着当您在 UIButton 上使用 UIAppearance 协议时,您所做的更改不会影响 UIBarButtonItem 的任何实例。

    NSObject >> UIBarItem >>UIBarButtonItem

    NSObject >> UIResponder >>UIView >> UIControl >> UIButton

    并且要回答您的第二个问题,如果您只希望将自定义应用于按钮的某些实例,那么您最好只创建 UIButton 的子类来自定义。通过这样做,您可以向子类添加逻辑以根据其父视图的类更改其外观。这可以通过使用类似的东西来完成:

    [self.superview isKindOfClass:[UITableViewCell class]];
    

    或者,如果您仍想使用 UIAppearance,请参阅 @null 的答案以获取示例。

    【讨论】:

    • 感谢您的回答!!!看看当我尝试使用 [self.superview isKindOfClass:[UITableViewCell class]] 时,它说:“在对象 AppDelegate 上找不到属性 'superview'”
    • @juandejesusss 这行不通。这是您将在 UIButton 的子类中使用的代码。
    【解决方案2】:

    您可以使用以下代理:

    [[UIButton appearanceWhenContainedIn:[MyView class], nil] setBackgroundColor:[UIColor greenColor]];
    

    更多信息请查看this

    顺便说一句,为UIButton 设置外观不会改变UIBarButtonItems 的外观。

    【讨论】:

    • 适用于单个类,但如果我需要两个或更多这样的格式怎么办:[[UIButton appearanceWhenContainedIn:[classVC1 class], [classVC2 class], nil]setTitleColor:(UIColorFromRGB( toolbarTextColor)) forState:UIControlStateNormal];这不起作用还是需要为每个类添加一行代码??
    • 这个视图数组表示包含层次结构,它不是容器列表,这样做你说的是包含在 classVC1 中的按钮,它包含在 classVC2 中。您需要拆分此数组。 '为每个类添加一行代码'.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多