iOS系统按钮单击框架问题



问题: 某些系统 UIButton 类型在其定义的框架/边界之外变得可单击。

以下两种按钮类型在其定义的框架区域内正确选择。

UIButtonTypeCustom [width:160, height:44.0]
UIButtonTypeSystem [width:50, height:44.0]

以下按钮类型甚至可以从其框架矩形外从四面八方选择10~15点。[在所有这些按钮的定义框架中绘制/显示背景颜色]

UIButtonTypeDetailDisclosure,[width:50, height:44.0]
UIButtonTypeInfoLight, [width:50, height:44.0]
UIButtonTypeInfoDark, [width:50, height:44.0]
UIButtonTypeContactAdd [width:50, height:44.0]

我希望可点击/可选区域应该在按钮框架中。

我使用以下方法创建按钮:

- (UIButton*)createButton:(UIButtonType)style withFrame:(CGRect)frame {
UIButton    *button = nil;
UILabel     *label  =   nil;
switch(style)
{
    case UIButtonTypeCustom: // frame: {x,y,160.0,44.0}
        button = [[UIButton alloc] initWithFrame:frame];
        button.layer.cornerRadius = 10;
        label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 44.0)];
        label.text = @"Custom";
        label.textAlignment = NSTextAlignmentCenter;
        [button addSubview:label];
        break;
    case UIButtonTypeSystem: // frame: {x,y,50.0,44.0}
        button = [UIButton buttonWithType:style];
        [button setTitle:@"Button" forState:UIControlStateNormal];
        button.frame = frame;
        break;
    default: // frame: {x,y,50.0,44.0}
        button = [UIButton buttonWithType:style];
        button.frame = frame;
        break;
}
// Background whitecolor is displaying properly for all buttons.
button.backgroundColor = [UIColor whiteColor];
return button;

}

创建按钮方法调用如下

for (int i=0, y=10, x=15; i<6; ++i) {
            if(i == 5)
            {
                y += 90;
                x = ([[UIScreen mainScreen] bounds].size.width - 160.0)/2.0;
            }
            UIButton *btn = [self createButton:5-i withFrame:CGRectMake(x, y, ( i<5 ? 50.0 : 160.0), 44.0)];
            [btn setTag: i+1];
            [btn addTarget:self action:@selector(settingsView:) forControlEvents:UIControlEventTouchUpInside];
            [_subView addSubview:btn];
            x += 60;
        }

iOS 通常根据自己的内部规则扩展按钮的可触摸区域。

我的猜测:它使用户更容易触摸按钮。

最新更新