简体   繁体   English

UIButton子类initWithCoder中的setTitleColor:forState未设置颜色

[英]setTitleColor:forState in UIButton subclass initWithCoder not setting color

I have subclassed a UIButton that I have included in a custom .xib file and I am trying to set the title colour for the button via the initWithCoder method for each state. 我已经将UIButton子类化,该UIButton已包含在自定义.xib文件中,并且我试图通过initWithCoder方法为每种状态设置按钮的标题颜色。 Currently, I have this: 目前,我有这个:

-(id)initWithCoder:(NSCoder *)aDecoder {
  self = [super initWithCoder:aDecoder];
  if(self) {
    self.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [self setTitleColor:[UIColor navigationLabelColor] forState:UIControlStateNormal];
  }
  return self;
}

However, the title colour is not changed but the edge insets are. 但是,标题颜色不会更改,但边缘插图会更改。 Am I doing something wrong? 难道我做错了什么?

PS: I should add, the custom xib is for a different element, but there are several instances of this button in that xib. PS:我应该补充一点,自定义xib是针对其他元素的,但是在该xib中有此按钮的多个实例。

Subclassing UIButton is not straight forward due to UIButtonType which seems to default to UIButtonTypeCustom for simple subclassing. 由于UIButtonType原因, UIButtonType子类化并不简单,对于简单的子类化而言, UIButtonTypeCustom似乎默认为UIButtonTypeCustom Changing the frame/bounds seems to work fine but some other aspects don't. 更改框架/边界似乎可以正常工作,但其他一些方面则不能。

Personally I have successfully subclassed for my needs which used UIButtonTypeCustom and mainly added background image stretching. 就我个人而言,我已经成功满足了我的需求,使用UIButtonTypeCustom并主要添加了背景图像拉伸。

You man need to find another method to accomplish your needs or delve deeper into subclassing UIButton. 您需要找到另一种方法来满足您的需求或深入研究UIButton的子类化。

Due a search on subclassing UIButton and you will find a substantial amount of information including comments saying that basically not to. 由于对子类UIButton进行了搜索,因此您会发现大量信息,其中包括注释,基本上没有。 See this SO answer . 看到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM