2013-10-17 40 views
2

在我的應用程序中,所有的UIButton對象都表現得很奇怪。看起來命中區域是兩倍大。所以當我點擊一個按鈕時,我實際上點擊了它旁邊或下面的按鈕。UIButton命中區域太大

我已經以編程方式以及Interface Builder創建了按鈕,但兩者的結果都是一樣的。

在我的應用程序中,我使用的是主題代理。但即使我禁用它,按鈕仍然表現得很奇怪。

這裏是創建按鈕的代碼:

_btOpenContact = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _btOpenContact.frame = Rect(0, CGRectGetHeight(self.view.bounds) - 200, 100, 100); 
    [_btOpenContact addTarget:self action:@selector(navigateToContact) forControlEvents:UIControlEventTouchUpInside]; 
    _btOpenContact.backgroundColor = RGB(233, 12, 24); resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 3, 0)] forState:UIControlStateNormal]; 
    [self.view addSubview:_btOpenContact]; 

任何人有什麼我做錯了任何想法?在此先感謝

更新

我添加了一個斷點按鈕之一:

<UIButton: 0x1e0a1220; frame = (0 348; 100 100); opaque = NO; layer = <CALayer: 0x1e0a11e0>> 

和它裏面的CALayer:

<CALayer:0x1e0a11e0; position = CGPoint (50 398); bounds = CGRect (0 0; 100 100); delegate = <UIButton: 0x1e0a1220; frame = (0 348; 100 100); opaque = NO; layer = <CALayer: 0x1e0a11e0>>; backgroundColor = <CGColor 0x1e0a1300> [<CGColorSpace 0x1d53db80> (kCGColorSpaceDeviceRGB)] (0.913725 0.0470588 0.0941176 1)> 
+0

_btOpenContact.frame ..? –

+0

抱歉...我的項目中有一些宏。 Rect實際上是CGRectMake(x,y,w,h) –

+0

可以添加這個 – manujmv

回答

0

儘管我仍然不知道是什麼導致了這個錯誤。 (也許是iOS7 sdk中的一個bug ...?)我通過繼承UIButton並做了一個命中測試來檢查我是否真的點擊了按鈕。

#import "TTButton.h" 

    @implementation TTButton {} 

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
    { 
     if (CGRectContainsPoint([self bounds], point)) { 
      NSLog(@"inside"); 
      return self; 
     } 

     return nil; 
    } 

    @end 
+0

不幸的是,這對我並不適用。 – VinceFior

0

我認爲你必須改變UIButton中使用的圖像大小,按鈕命中區域在uibutton框架下,但圖像大小小於按鈕框架。

+0

即使我沒有使用圖像,問題仍然會發生。 –

0

將您的按鈕寬度設置爲320意味着它將覆蓋整個屏幕,將寬度和高度更改爲100 * 100並設置任何背景顏色以進行測試。通過查看按鈕的背景顏色,您會明白按鈕的可見區域是什麼。

+0

試過了,但仍然有同樣的問題。我用新的(測試)代碼更新了我的問題 –

+0

根據您的代碼,可見按鈕是否正確? – Husyn

+0

是按鈕可見[button.png](http://im3d.nl/screens/button.png) –