iOS系统按钮点击框问题

标签 ios objective-c uibutton uikit

问题: 某些系统 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;

createButton方法调用如下

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 通常会根据自己的内部规则扩展按钮的可触摸区域。

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

关于iOS系统按钮点击框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27181859/

相关文章:

ios - 在 Swift 的导航栏上为 UIView 设置动画

ios - 是否可以在非 View 类中创建 IBOutlet?

objective-c - 我可以重复使用我的应用程序 IPA、编辑其中的资源并使用脚本更新 CodeResources 吗?

ios - 检测 NSString 的语言

ios - objc_msgSend EXC_BAD_ACCESS 使用关闭按钮时

ios - 具有自动调整单元格大小的自定义 UICollectionViewLayout 会因较大的估​​计项目高度而中断

ios - 如何在头文件中声明公共(public) NS_Enum 属性

ios - 在用户点击按钮之前设置一个计时器 ios

ios - UIButtons 不粘在 UIView 边缘

ios - Xcode 日期格式 - 来自 RSS 的格式