2013-03-05 37 views
1

我有一個自定義UIView作爲子視圖的一些按鈕。我閱讀了很多關於我的問題的主題,但發佈的解決方案對我並不適用。自定義UIView UIButtons沒有響應(再次)

如果我將其中一個按鈕的框架設置爲我的UIView之外的某些座標,該按鈕將起作用。當我將其中一個按鈕的框架設置爲自定義UIView和按鈕重疊的某些座標時,按鈕僅在它們不重疊的區域作出響應。

這是我的代碼:

我初始化viewDidLoad方法自定義的UIView:

[super viewDidLoad]; 

DDButtonBar *ddButtonBar = _ddButtonBar; 

[ddButtonBar showButton1:TRUE]; 
[ddButtonBar showButton2:TRUE]; 
[ddButtonBar showButton3:TRUE]; 
[ddButtonBar showButton4:TRUE]; 
[ddButtonBar showBackButton:TRUE]; 
[ddButtonBar showSettingsButton:TRUE]; 

[ddButtonBar setButtonTitle1:@"1"]; 
[ddButtonBar setButtonTitle2:@"2"]; 
[ddButtonBar setButtonTitle3:@"3"]; 
[ddButtonBar setButtonTitle4:@"4"]; 

這是我的UIView的init方法:

- (id)initWithFrame:(CGRect)frame 

self = [super initWithFrame:frame]; 
if (self) { 

    // Standardinitialisierung 
    _drawButton1 = TRUE; 
    _drawButton2 = TRUE; 
    _drawButton3 = TRUE; 
    _drawButton4 = TRUE; 
    _drawBackButton = TRUE; 
    _drawSettingsButton = TRUE; 

    _buttonTitle1 = @"1"; 
    _buttonTitle2 = @"2"; 
    _buttonTitle3 = @"3"; 
    _buttonTitle4 = @"4"; 

    [self layoutSubviews]; 


} 
return self; 

}

我畫我的按鈕一樣,

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button1 addTarget:self 
      action:@selector(aMethod:) 
    forControlEvents:UIControlEventTouchUpInside]; 
[button1 setTitle:_buttonTitle1 forState:UIControlStateNormal]; 

button1.frame = CGRectMake(rechtsverschiebung, height, buttonWidth, height); 

[button1 setBackgroundImage:_buttonBackground forState:UIControlStateNormal]; 
[button1 setTitleColor:[UIColor colorWithRed:255.0 green:255.0 blue:255 alpha:1.0] forState: UIControlStateNormal]; 
[button1 setTitleColor:[UIColor colorWithRed:247.0 green:165.0 blue:32 alpha:1.0] forState: UIControlStateHighlighted]; 

[self addSubview:button1]; 
+0

[self.view bringSubviewtofront:];試試這個 – amar 2013-03-05 09:36:44

+0

我已經試過了,我會發布我完全做的。我在我的viewDidLoad [self.view bringSubviewToFront:_ddButtonBar]中試過這個; ,這在我的自定義UIView [self bringSubviewToFront:button1]; 我無法在自定義UIView中使用[self.view ...]。 – user2135074 2013-03-05 09:51:14

+0

最佳選擇是爲所有視圖提供不同的顏色查看哪個視圖位於按鈕上方,或者您的按鈕矩形可能會超出視圖的矩形區域 – amar 2013-03-05 10:22:40

回答

0

檢查下button.In的y位置設置你所有按鈕相同的Y位置代碼。

+0

我的用戶界面看起來像這樣,所以我認爲位置應該是OK並且它們不重疊。 http://imageshack.us/a/img600/4877/iossimulatorbildschirmf.png – user2135074 2013-03-05 10:21:31

+1

我發現了這個問題。我的按鈕沒有被放置在實際的UIView區域,被按下了一些像素。謝謝 :) – user2135074 2013-03-05 10:25:19