2012-10-02 65 views
0

我試圖在自定義UIView上放置自定義UIButton,但未顯示UIButton。儘管當我嘗試打印我的UIViewUISubViews時,它在那裏顯示了UIButton對象。顯示自定義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

這是一個非常糟糕的主意,做任何內部drawRect,你不必絕對必須如繪圖。它要慢得多,並且經常會有奇怪的行爲。也就是說,如果你註釋掉你的drawRect函數並在init函數中設置你的按鈕,它會顯示出來嗎? – SethHB

回答

0

所以我找到了你需要的東西!

也許這:

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]; 

現在ü可以添加你的選擇!嘗試一下!

這裏是我的應用程序的一部分東西,u能明白我的意思:

- (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]; 
+0

好的。現在,當我將按鈕類型更改爲「UIButtonTypeRoundedRect」時,我可以看到按鈕出現在那裏,但沒有圖像。 – Abhinav

+0

你是否按照這些代碼? – Noah

+0

閱讀最終解決方案! – Noah

1

我的線索:

1.爲什麼不ü用故事板?可能是alpha?!

嘗試使用:

self.alpha = 1.0; 

希望它的工作:d

+0

沒有。阿爾法是好的,因爲我可以看到我的自定義視圖正常,但它不帶有按鈕:-(。 – Abhinav

+0

什麼是xib? – Noah

+0

我沒有使用XIB文件。 – Abhinav

-1

-initWithFrame:可能不會得到所謂的,這取決於你如何創建按鈕。 -initWithCoder:如果您在.xib文件中創建按鈕,可能是被調用的。

0

您需要爲您的按鈕添加背景顏色 - 直到您設置它爲止。

0

繼一段代碼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]; 
    } 
0

你的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];