![](/img/trans.png)
[英]Strange issue with numeric keyboard losing custom UIButton when it finishes displaying
[英]Issue in displaying custom UIButton
我試圖將自定義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];
}
有人知道我在做什么錯嗎?
我的提示:
1.為什么不使用情節提要?
2.可能是alpha ?!
嘗試使用:
self.alpha = 1.0;
希望它有用:D
您需要為按鈕添加背景顏色-在您進行設置之前,該顏色為空。
所以我找到了你所需要的!
也許這:
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];
以下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];
}
您的actionButton的initWithFrame在哪里?
抱歉,我懶得瀏覽我的代碼,但這是Internet上如何創建自定義按鈕的示例。
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];
-initWithFrame:
可能不會被調用,具體取決於您如何創建按鈕。 如果在.xib文件中創建按鈕,則-initWithCoder:
可能會被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.