這是一個很常見的問題:你的UIButton超出了它的一個superviews的界限。如果clipsToBounds
/masksToBounds
設置爲NO(默認值),您的UIButton仍然會顯示,但觸摸事件不會被髮送到它。
讓我們來簡化這種情況。假設一個視圖控制器用下面的代碼:
- (void) viewDidLoad
{
[super viewDidLoad];
UIColor *fadedRedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.25];
UIColor *fadedBlueColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.25];
CGRect containerFrame = CGRectMake(25, 25, 100, 100);
CGRect buttonFrame = CGRectMake(100, 100, 64, 44);
UIView *container = [[UIView alloc] initWithFrame:containerFrame];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setFrame:buttonFrame];
[button addTarget:self action:@selector(_handleButton:) forControlEvents:UIControlEventTouchUpInside];
[container setBackgroundColor:fadedRedColor];
[button setBackgroundColor:fadedBlueColor];
[container addSubview:button];
[[self view] addSubview:container];
}
- (void) _handleButton:(id)sender
{
NSLog(@"Moooooo!");
}
看起來像這樣: ![Screenshot 1](https://i.stack.imgur.com/6YnEt.png)
按鈕包含在container
,但它駐留在容器的邊界的外側(容器是100個像素寬和100像素高,按鈕的原點在100, 100
)。
當您觸摸屏幕時,UIKit將從視圖層次結構(UIWindow)的頂部開始遞歸調用-[UIView hitTest:withEvent:]
,直至找到應處理該觸摸的視圖。然而,在這個例子中,UIKit永遠不會下降到容器中(因爲你觸摸了它的邊界),因此按鈕子視圖將不會被擊中。
如果我們不是改變buttonFrame
是50, 50
,它看起來像這樣: ![Screenshot 2](https://i.stack.imgur.com/OhgFn.png)
與容器重疊將響應觸摸事件按鈕的一部分。駐留在容器外的部分將不會:
![Screenshot 3](https://i.stack.imgur.com/s3r1P.png)
要調試一個觀點,即不完全可觸摸,你可以試試下面這樣的調試功能:
static void sDebugViewThatIsntTouchable(UIView *view)
{
UIView *superview = [view superview];
while (superview) {
CGRect rectInSuperview = [view convertRect:[view bounds] toView:superview];
CGPoint topLeft = CGPointMake(CGRectGetMinX(rectInSuperview), CGRectGetMinY(rectInSuperview));
CGPoint topRight = CGPointMake(CGRectGetMaxX(rectInSuperview), CGRectGetMinY(rectInSuperview));
CGPoint bottomLeft = CGPointMake(CGRectGetMinX(rectInSuperview), CGRectGetMaxY(rectInSuperview));
CGPoint bottomRight = CGPointMake(CGRectGetMaxX(rectInSuperview), CGRectGetMaxY(rectInSuperview));
if (![superview pointInside:topLeft withEvent:nil]) {
NSLog(@"Top left point of view %@ not inside superview %@", view, superview);
}
if (![superview pointInside:topRight withEvent:nil]) {
NSLog(@"Top right point of view %@ not inside superview %@", view, superview);
}
if (![superview pointInside:bottomLeft withEvent:nil]) {
NSLog(@"Bottom left point of view %@ not inside superview %@", view, superview);
}
if (![superview pointInside:bottomRight withEvent:nil]) {
NSLog(@"Bottom right point of view %@ not inside superview %@", view, superview);
}
superview = [superview superview];
}
};
編輯: 正如你在評論中提到的,罪魁禍首是UIWindow的主要尺寸爲320x480而不是320x568。 打開xib中的「啓動時全屏」固定此。
當然,問題是:「爲什麼?」 :)
如果您在文本編輯器中拉出xib文件,您會注意到寬度320和高度480被硬編碼到窗口。當xib在啓動時解碼時,窗口最初是用這個320x480幀構造的。
UIKit然後查詢-[UIWindow resizesToFullScreen]
(私有方法)。如果這返回YES,則UIWindow執行[self setFrame:[[self window] bounds]]
的等效操作。
在Interface Builder中切換「啓動時全屏」標誌直接切換私有UIWindow.resizesToFullScreen
標誌。
我認爲這是幀大小的問題。因此,請嘗試檢查視圖大小(在您嘗試添加按鈕時檢查視圖幀) – Tendulkar
嘗試在設備上嘗試其他任何操作。 – preetam
我沒有用於設備測試的iPhone 5。 –