【问题标题】:iOS system button click frame issueiOS系统按钮点击框问题
【发布时间】:2014-11-28 04:23:00
【问题描述】:

问题: 某些系统 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 objective-c uibutton uikit


    【解决方案1】:

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

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

    【讨论】:

    • 我已经给了他们足够的框架宽度和高度供选择。 UIButtonTypeSystem 确实具有相同的宽度和高度,并且表现正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2015-05-09
    • 2019-03-11
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多