【问题标题】:Hit target on UIToolbar with custom-image buttons is not correct使用自定义图像按钮在 UIToolbar 上命中目标不正确
【发布时间】:2010-08-17 19:42:18
【问题描述】:

我有一个使用我自己的背景图像自定义的 UIToolbar。因此,内置的 UIBarButtonItem 外观对我不起作用,所以我使用的是已经准备好在栏中显示的图像。我用这种方法创建了一个自定义按钮项:

+ (UIBarButtonItem *)customWithImage:(UIImage *)image enabled:(BOOL)enabled target:(id)target action:(SEL)selector {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //I've tried different values for the frame here, but no luck
button.frame = CGRectMake(0, 0, 44, 44);
button.enabled = enabled;
button.showsTouchWhenHighlighted = YES;
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];

UIBarButtonItem *it = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    //Tried changing this, to no avail
it.width = 32.f;
return it;

我在左边有一个按钮,在右边有一个按钮,我正在努力做到这一点,以便如果您点击 UIToolbar 的最左侧或最右侧,则会点击相应的按钮。但是,使用这些自定义按钮,命中目标不会一直延伸到 UIToolbar 的边缘,而是从侧面插入:

http://skitch.com/andpoul/d1p8g/hit-targets

非常感谢任何帮助!

【问题讨论】:

    标签: iphone uibarbuttonitem uitoolbar


    【解决方案1】:

    如果您使用自定义视图(它可能只使用视图的宽度),UIBarButtonItem.width 可能会被忽略。

    一个蹩脚的技巧是使工具栏更宽(因此它粘在屏幕外)并在背景图像中添加透明边缘以进行补偿。这会使按钮更靠近屏幕边缘。

    另一种方法是使用带有 UIButton 子视图的 UIImageView。

    【讨论】:

    • 这是我决定的蹩脚黑客。我知道这很糟糕,但是,嘿,解决了这个问题。在我的 UIToolbar 子类中,我将 layoutSubviews 中按钮的框架调整为更合理。我尽量保持保守,这样它就不会在未来的操作系统上完全吃掉它。
    【解决方案2】:

    我认为您必须采取的唯一方法是使按钮更宽(通过从左侧添加一些,从右侧添加一些来更改图像)并调整大小...

    【讨论】:

    • 使按钮变宽也无济于事,因为左侧和右侧仍有空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多