【问题标题】:iPhone: How to make UIButton which you can easily tap?iPhone:如何制作可以轻松点击的 UIButton?
【发布时间】:2012-06-26 17:10:18
【问题描述】:

我正在使用UIViewUIButtons 而不是UINavigationBar 制作自定义导航栏。

但我在导航栏上的UIButtons 没有敏感响应。

我必须点击UIButton 的几乎中心才能点击。
如果我点击UIButton 的边缘,它不会响应。

但是普通UINavigationBar上的按钮可以通过点击按钮的边缘来点击。
即使在按钮外部点击,也可以点击。

相机应用程序上的快门按钮或选项按钮也可以通过点击按钮的边缘或外部来点击。

如何在我的应用中实现那些易于点击的按钮?

【问题讨论】:

  • 一些示例代码可能有助于诊断问题。
  • @ben 谢谢。我会寻找示例代码。但这不是我的问题。我认为这是UIButton 的规范。所有UIButton 都没有UINavigationBar 的按钮敏感。我想知道一些可以挖掘的属性或扩展区域的方法。
  • UIButton 通常有一个非常大的点击区域。您可能错误地配置了它们,而不是它们被破坏了,您需要解决它。 Ben 的代码请求是正确的。
  • @Rob 不是很大。请点击并比较示例代码的UIButton和导航栏按钮,例如UICatalogAddMusic与真机而非模拟器。

标签: iphone ios uikit uibutton


【解决方案1】:

使用图像并创建自定义按钮。设置按钮,使图像不会缩放到按钮视图的大小,而只是居中。扩大按钮的大小,使其大于每一侧的图像。 Apple 在标签按钮之类的东西上也做到了这一点。

【讨论】:

  • 为了澄清一下,按钮的实际尺寸比它显示的图像大,所以可触摸区域比图像大小要大很多,虽然由于只显示图像,所以要大很多区域是触摸敏感的。
  • 谢谢!有用。我发现 UIButton 的 inset 属性对于扩展点击区域也很有用。并且为图片添加透明边距也可以扩大点击区域。
【解决方案2】:

UIButton 有一个 imageEdgeInsets property 专门用于此目的。只需使用imageEdgeInsets 为可触摸区域制作一个与您需要的一样大的 UIButton 框架,然后适当地缩放其中的图像。

【讨论】:

    【解决方案3】:

    免责声明: 此代码未经测试,但它让您了解如何完成。

    您制作一个按钮(在本例中为 40px x 40px),然后向其添加一个较小的背景图像,因此给人的印象是该图像非常“可点击”。

    // This image is 20px x 20px (Just an example)
    UIImage* backgroundImage = UIImage imageNamed:@"backgroundImage.png"]
    
    // Custom button, remember to add a target method
    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    customButton.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    customButton.contentMode = UIViewContentModeCenter;
    [customButton setImage:backgroundImage forState:UIControlStateNormal];
    
    UIBarButtonItem* customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
    self.navigationItem.rightBarButtonItem = customBarButtonItem;
    [customBarButtonItem release];
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多