2013-01-20 30 views
2

我已將一個UIView添加到視圖控制器的視圖。你說:如何使UIView的子視圖放置在UIView框架之外,是交互式的?

CGRect paneRect = {10.0f, 10.0f, 300.0f, 50.0f}; 
UIView *pane = [[UIView alloc] initWithFrame:paneRect]; 
pane.backgroundColor = [UIColor greenColor]; 
pane.userInteractionEnabled = YES; 
pane.clipsToBounds = NO; 
[self.view addSubview:pane]; 

然後我添加一個UIButton到pane

CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f}; 
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[test setTitle:@"test" forState:UIControlStateNormal]; 
[test setFrame:testRect]; 
[pane addSubview:test]; 

現在一半的「測試」按鈕是窗格內,另一半超出。上半部分是互動的,並且對接觸做出反應,但下半部分不是。

enter image description here

是否有可能使整個「測試」按鈕,互動性和可觸摸,而不是它的一半?

+0

您可以添加'test'到'self.view'無論如何,除非你有其他要求... – nhahtdh

+0

具有u檢查我的答案 –

+0

@DixitPatel嗨,我給你的答案嘗試但不解決問題。 – anonim

回答

0

如果你真的想使這些觀點堅持彼此,那麼你可以做這樣的事情

CGRect mainViewRect = {10.0f, 10.0f, 300.0f, 75.0f}; 
UIView *mainView = [[UIView alloc] initWithFrame:mainViewRect]; 
mainView.backgroundColor = [UIColor clearColor]; 
mainView.userInteractionEnabled = YES; 
[self.view addSubview:mainView]; 

CGRect paneRect = {0.0f, 0.0f, 300.0f, 50.0f}; 
UIView *pane = [[UIView alloc] initWithFrame:paneRect]; 
pane.backgroundColor = [UIColor greenColor]; 
pane.userInteractionEnabled = YES; 
pane.clipsToBounds = NO; 
[mainView addSubview:pane]; 



CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f}; 
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[test setTitle:@"test" forState:UIControlStateNormal]; 
[test setFrame:testRect]; 
[mainView addSubview:test]; 
+0

我正在開發一個**自動完成文本字段**,並且當建議表格視圖在文本框中繪製時,字段,我試圖使它成爲文本字段的子視圖。一切工作正常,除了UITableView沒有收到觸摸事件,我不知道該如何處理。我想最後我必須遵循你的解決方案。 – anonim

0

只是不喜歡這種方式

CGRect paneRect = {10.0f, 10.0f, 300.0f, 50.0f}; 
UIView *pane = [[UIView alloc] initWithFrame:paneRect]; 
pane.backgroundColor = [UIColor greenColor]; 
pane.userInteractionEnabled = NO; 
pane.clipsToBounds = NO; 
[self.view addSubview:pane]; 


CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f}; 
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[test setTitle:@"test" forState:UIControlStateNormal]; 
test.userInteractionEnabled = YES; 
[test setFrame:testRect]; 
[pane addSubview:test]; 

對於UIButton的行動只是增加

[test addTarget:self action:@selector(processButtonCLick) forControlEvents:UIControlEventTouchUpInside]; 

如果你想UIButton的窗格的中心添加

test.center =pane.center; 
+0

感謝您的建議,但現在應用「測試」按鈕後根本不具有互動性。實際上'test'按鈕是'窗格'視圖的子視圖。如果'pane'設置爲不交互的,它的所有子節點都不會收到觸摸事件(我當然想知道) – anonim

相關問題