【问题标题】:UIButton title disappearsUIButton 标题消失
【发布时间】:2012-02-16 14:20:07
【问题描述】:

我有一个 ViewController,里面有一个 UIButton。 当我点击时,按钮上的文字消失了。 我为所有状态添加了所有 setTitle,但它继续消失。 有什么想法吗?

这是我的代码的一部分:

@interface AddCardViewController : UITableViewController <UITextFieldDelegate>{
UIButton *commit;
    ......

@implementation AddCardViewController

- (void)viewDidLoad{

    self.commit = [UIButton buttonWithType: UIButtonTypeCustom];

    [self setCommitProperties];

    [self.view addSubview:commit];

 .........}



- (void) setCommitProperties{

   CGRect frameTable = self.tableView.frame;

   CGRect frame = CGRectMake(frameTable.origin.x + 10, 140, frameTable.size.width - 20, 40);

   commit.frame = frame;

   [commit setBackgroundColor : [UIColor whiteColor]];

   [commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateNormal];
   [commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateSelected];
   [commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateHighlighted];
   [commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateApplication];
   [commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateReserved];
   [commit setTitle: NSLocalizedString(@"AddCard",@"") forState: UIControlStateDisabled];

    [commit addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];    

   UIColor *color = [[[PersonalizationManager alloc] init] getColor: @"AddCardViewController" :@"Commit_Title"]; 

   [commit.titleLabel setTextColor: color];

   color = [[[PersonalizationManager alloc] init] getColor: @"AddCardViewController" :@"Commit_Border"];

   [commit.layer setBorderColor:[color CGColor]];    
   [commit.layer setBorderWidth : 0.5f];
   [commit.layer setCornerRadius : 10.0f];
}

【问题讨论】:

    标签: iphone objective-c uibutton


    【解决方案1】:

    嗯,这很难确定,但文字是因为颜色相同而看不到吗?我看到您为所有状态设置了文本,但您可能还想为所有状态设置颜色。

    [commit setTextColor:[UIColor redColor] forState:UIControlStateSelected];
    

    【讨论】:

    • 是的!这就是解决方案。你说的对。我没有考虑颜色,我尝试并测试了整个标题的属性。这些都是问的好东西。非常感谢
    • 我是一个新的 ios 开发人员,我认为 api 已更改 cz 没有可用的 setTextColor 方法。改用 setTitleColor
    【解决方案2】:

    使用属性文本,确保您的 storboard/xib 中的按钮类型 = 自定义(不是系统)。

    【讨论】:

      【解决方案3】:

      我注意到一件奇怪的事情。 如果我使用

      设置标题
        bttn.titleLabel.textColor = [UIColor purpleColor];
      

      然后,只要我单击按钮,标题就会消失。 但是,如果我使用以下方法:

      [bttn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
      

      然后一切正常。

      【讨论】:

        【解决方案4】:

        我猜你需要使用 [self.commit setTitle: NSLocal...,实际上使用 self.commit 来完成分配(当你使用 @property (nonatomic, retain)strong 和 ARC 时)。我使用@synthesize commit = _commit;,然后对模块的所有其余部分只使用_commit。

        【讨论】:

          【解决方案5】:

          正如杰克所说,按钮的“选定”状态具有与背景相同的颜色。 您也可以在情节提要中更改该颜色:

          • 选择按钮
          • 在 Attributes Inspector -> State Config -> 选择“highlighted”
          • 选择正确的“文本颜色”

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-14
            • 2018-07-09
            • 2014-12-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多