2013-05-14 33 views
0

這裏是我的工作代碼:子視圖用的UIButton無法點擊

UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
[mainButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
mainButton.center = CGPointMake(self.view.frame.size.width/2.0f, self.view.frame.size.height/2.0f); 
[self.view addSubview:mainButton]; 

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)]; 
[subView setBackgroundColor:[UIColor purpleColor]]; 
[mainButton addSubview:subView]; 

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
secondButton.center = CGPointMake(subView.frame.size.width/2.0f, subView.frame.size.height/2.0f); 
[subView addSubview:secondButton]; 

爲什麼mainButton點擊,但secondButton是不是?

+1

主按鈕的框架是100乘100,子視圖就是那麼多。所以第二個按鈕會熄滅。但是,正是你想用這個做什麼? – Durgaprasad 2013-05-14 12:01:51

+1

何時在self.view中添加子視圖,然後這兩個按鈕將可點擊 – Rushabh 2013-05-14 12:06:05

+0

謝謝,我會盡力實現這一點。我正在創建一個UIView子類,該UIBiew子類具有一個UIButton,當它按下時,會在mainButton的右側顯示一個選擇欄,其中有更多的按鈕可供選擇。這個DID工作。謝謝你們的幫助 – Parad0x13 2013-05-14 12:10:33

回答

2

K你應該在self.view上添加子視圖。

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)]; 
[subView setBackgroundColor:[UIColor purpleColor]]; 
[self.view addSubview:subView]; 

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
secondButton.center = CGPointMake(subView.frame.size.width/2.0f, subView.frame.size.height/2.0f); 
[subView addSubview:secondButton]; 
UIButton *thirdButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 0, 100, 100)]; 
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
secondButton.center = CGPointMake(subView.frame.size.width/2.0f, subView.frame.size.height/2.0f); 
[subView addSubview:secondButton]; 

試試這個。它會在子視圖上添加兩個按鈕。

+0

我需要這兩個按鈕在不同的子視圖上,以我在做的事情 – Parad0x13 2013-05-14 12:54:02

+0

的方式工作在主要按鈕添加按鈕的目標方法中(2/3 ...)子視圖並將子視圖添加到self.view。 – Durgaprasad 2013-05-14 13:01:32

+0

這是一個有趣的做法Durgaprasad ...而不是放置子視圖'sliderView'它包含按鈕到父UIButton我可以使'sliderView'部分的UIMultiButton子類和調用[[theMultiButtonSubclass parentView] insertSubview:sliderView belowView:this]或者其他東西 – Parad0x13 2013-05-15 23:15:58

-1

問題是你的subView是mainButton的一個孩子。您應該將其添加到viewController的視圖。

0

你不應該在UIButton的

添加的UIView作爲子視圖

試試這個

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)]; 
[subView setBackgroundColor:[UIColor purpleColor]]; 
[self.view addSubview:subView]; 

UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
[mainButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
mainButton.center = CGPointMake(self.view.frame.size.width/2.0f, self.view.frame.size.height/2.0f); 
[subView addSubview:mainButton]; 

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 100)]; 
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
[subView addSubview:secondButton]; 

編輯:如果你想保持不同意見的兩個按鈕再取兩個子視圖和嘗試像....

UIView *subView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)]; 
[subView1 setBackgroundColor:[UIColor purpleColor]]; 
[self.view addSubview:subView1]; 

UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
[mainButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
[subView1 addSubview:mainButton]; 


UIView *subView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)]; 
[subView2 setBackgroundColor:[UIColor purpleColor]]; 
[self.view addSubview:subView2]; 


UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal]; 
[subView2 addSubview:secondButton]; 
+0

謝謝,但我需要他們在不同的子視圖 – Parad0x13 2013-05-14 12:54:30

+1

歡迎來到編輯我已經給出了答案這可能對你有所幫助......享受編碼.... !!!!! ! – 2013-05-14 13:00:54

+0

太棒了!我會喜歡的編碼:D一如既往 – Parad0x13 2013-05-14 14:46:26

相關問題