【问题标题】:Title of a Custom UIBarButtonItem from a UIButton not showing来自 UIButton 的自定义 UIBarButtonItem 的标题未显示
【发布时间】:2014-09-01 10:24:22
【问题描述】:

我从UIButton 自定义了UIBarButtonItem。自定义栏按钮可见,动作有效,大小正确,可以看到背景颜色;但是无法看到手动设置的标题。也许背景覆盖在文本上?我不确定,帮忙?

viewDidLoad:

UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:@selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:@"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = @"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;

UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];

self.navigationItem.rightBarButtonItem = resetBarBTN;

【问题讨论】:

    标签: ios uibutton uibarbuttonitem


    【解决方案1】:

    使用 setTitle:forState 设置按钮标题。例如

    [resetButton setTitle:@"text" forState:UIControlStateNormal]
    

    【讨论】:

      【解决方案2】:

      您应该为特定控制状态使用setTitle:forState:的按钮设置标题,用下面的代码替换您的代码

      UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
      resetButton.frame = CGRectMake(0, 0, 65, 30);
      [resetButton addTarget:self action:@selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
      resetButton.titleLabel.font = [UIFont fontWithName:@"ProximaNova-Bold" size:19];
      
      [resetButton setTitle:@"RESET" forState:UIControlStateNormal]; //change this line in your code
      
      resetButton.titleLabel.textColor = [UIColor whiteColor];
      resetButton.backgroundColor = [UIColor redColor];
      resetButton.showsTouchWhenHighlighted = YES;
      
      UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
      
      self.navigationItem.rightBarButtonItem = resetBarBTN;
      

      【讨论】:

        猜你喜欢
        • 2016-11-21
        • 2020-06-29
        • 2021-09-21
        • 1970-01-01
        • 2016-02-29
        • 2015-01-07
        • 1970-01-01
        • 1970-01-01
        • 2011-11-05
        相关资源
        最近更新 更多