【问题标题】:Change UIButton's default titleColor更改 UIButton 的默认 titleColor
【发布时间】:2014-04-28 11:51:34
【问题描述】:

我正在寻找一种在应用启动时将UIButton's 默认titleColor 设置为自定义的简单方法。但是我想尊重这种情况,当titleColor 不是默认值并且具有自定义值时。 [[UIButton appearance] setTitleColor: forState:] 允许我一次为所有按钮设置自定义颜色。但是它会覆盖已经设置的自定义颜色。 有没有什么快速的方法可以同时覆盖默认标题颜色并尊重自定义颜色?

【问题讨论】:

  • [按钮 setTitleColor:[UIColor YourColor] forState:UIControlStateNormal];请在提问前搜索您的答案
  • 以这种方式,我需要每次在每个控制器上以编程方式设置按钮的颜色。但我希望在应用程序启动时覆盖默认颜色。但是对于这种情况,当界面生成器中的颜色已经设置为自定义值(不是默认颜色选项)时,不应覆盖自定义颜色。

标签: ios uibutton


【解决方案1】:

使用appearance 设置全局外观(所有按钮)和实例方法 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 为一个按钮设置颜色。

【讨论】:

  • @nyekimov 这是你需要的吗?
  • 是的,伙计。这真的是我需要的。我尝试通过界面生成器自定义设置颜色,显然外观超过了它。但是,您的程序化建议很有效。
  • @nyekimov 我很高兴听到这个消息。如果有效,那么您应该接受我的回答。
【解决方案2】:
    Try to this.....

    create catagory class for UIButton like bellow

    Sample.h
    #import <UIKit/UIKit.h>

    @interface Sample : UIButton

    @end

    Sample.m
    #import "Sample.h"

    @implementation Sample
    -(id)initWithCoder:(NSCoder *)aDecoder
    {
        self=[super initWithCoder:aDecoder];
        if (self) {
            [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        return self;
    }
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }

    @end


    and After crate class use this class as Custom Class Like

![Image][1]


  [1]: http://i.stack.imgur.com/sXYfY.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多