显示自定义 UIButton 的问题
Posted
技术标签:
【中文标题】显示自定义 UIButton 的问题【英文标题】:Issue in displaying custom UIButton 【发布时间】:2012-10-02 23:57:47 【问题描述】:我正在尝试将自定义 UIButton
放在自定义 UIView
上,但 UIButton
没有出现。虽然当我尝试打印我的UIView
的UISubViews
时,它会在那里显示UIButton
对象。
下面是我正在编写的将按钮放在我的自定义 UIView
上的代码
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
self.backgroundColor = [UIColor blackColor];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.textColor = [UIColor whiteColor];
self.headerLabel.font = [UIFont systemFontOfSize:kApplicationHeaderTextFont];
[self addSubview:self.headerLabel];
self.actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.actionButton.frame = CGRectZero;
[self addSubview:self.actionButton];
return self;
- (void)drawRect:(CGRect)iTotalRect
if (self.actionButtonImage)
self.actionButton.frame = CGRectMake(self.frame.size.width - self.actionButtonImage.size.width - 10.0, self.frame.size.height / 2 - self.actionButtonImage.size.height / 2, self.actionButtonImage.size.width, self.actionButtonImage.size.height);
[self.actionButton setImage:self.actionButtonImage forState:UIControlEventTouchUpInside];
有人知道我做错了什么吗?
【问题讨论】:
在 drawRect 中做任何你不需要做的事情是一个非常糟糕的主意,例如绘图。它要慢得多,并且经常会出现奇怪的行为。也就是说,如果你注释掉你的 drawRect 函数并在你的 init 函数中设置你的按钮,它会显示吗? 【参考方案1】:我的线索:
1.你为什么不用故事板??
2.可能是alpha?!
尝试使用:
self.alpha = 1.0;
希望它有效:D
【讨论】:
没有。 Alpha 没问题,因为我可以看到我的自定义视图很好,但它上面没有按钮 :-(. 发布了一个新的解决方案寻找! 当我将图像设置为背景图像而不是将其作为图像时,它对我有用。我在此处的解决方案中添加了更改后的代码。非常感谢您的宝贵指导。【参考方案2】:所以我找到了你需要的东西!!
也许是这样的:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
现在你可以添加你的选项了!试试看!
这是我的应用程序的一部分,你可以明白我的意思:
- (void)addMyButton // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:@"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
我为你找到了最终解决方案:
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];
【讨论】:
好的。现在,当我将按钮类型更改为“UIButtonTypeRoundedRect”时,我可以看到按钮出现在那里,但上面没有图像。【参考方案3】:你需要给你的按钮添加一个背景色——在你设置它之前它是空的。
【讨论】:
【参考方案4】:drawRect 中的以下代码对我有用。所以,基本上使用自定义按钮你不应该设置图像,你应该设置背景图像。
- (void)drawRect:(CGRect)iTotalRect
if (self.actionButtonImage)
self.actionButton.frame = CGRectMake(self.frame.size.width - self.actionButtonImage.size.width - 10.0, self.frame.size.height / 2 - self.actionButtonImage.size.height / 2, self.actionButtonImage.size.width, self.actionButtonImage.size.height);
[self.actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.actionButton setBackgroundImage:self.actionButtonImage forState:UIControlStateNormal];
【讨论】:
【参考方案5】:actionButton 的 initWithFrame 在哪里?
抱歉,我懒得看我的代码,但这里是来自互联网的示例,说明如何创建自定义按钮。
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:@"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
UIImage *strechableButtonImageNormal =
[buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
UIImage *strechableButtonImagePressed =
[buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton addTarget:self
action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
【讨论】:
【参考方案6】:-initWithFrame:
可能不会被调用,这取决于您创建按钮的方式。如果您在 .xib 文件中创建按钮,则可能会调用 -initWithCoder:
。
【讨论】:
以上是关于显示自定义 UIButton 的问题的主要内容,如果未能解决你的问题,请参考以下文章
iPhone SDK:带有自定义图像的 UIButton 可以显示标题吗?
iOS View自定义窍门——UIButton实现上显示图片,下显示文字