ios - xcode iOS 增加UIButton点击区域

标签 ios uibutton xcode6

我正在尝试增加我的 UIButton 的点击区域。我有一个新的 Xcode 项目,在 Storyboard的中央有一个 UIButton。该按钮通过 socket 和操作连接到我的 View Controller 。我能够以编程方式更改按钮标题,只是为了好玩,当我点击按钮时,它的 Action 会更改 View 的背景。所以我知道一切都已连接并且正常工作。

根据我在网上阅读的内容,这应该可行,但事实并非如此。

[按钮设置框架:CGRectMake(button.frame.origin.x, button.frame.origin.y, -64, -64)];

有什么想法吗?我试过没有负边距,但那也不管用。顺便说一句,我已经尝试了很多问这个问题的地方,但它没有用。谢谢您的帮助。另外,我听说子类化 UIButton 可能会产生问题,对此有什么想法吗?

更新: 我想要制作的 UIButton 具有背景颜色并且是特定大小。我想保持该大小不变,但在不扭曲按钮外观的情况下增加其周围的点击区域。

最佳答案

我想你只是想使用相同的按钮将背景颜色或背景图像限制在同一区域并增加触摸区域?

据我所知,您不能在界面生成器中执行此操作。但是通过编码,您可以使用图层。

取出按钮

  CALayer *buttonlayer=[CALayer layer];

  buttonlayer.frame=CGRectMake(50 , 50, 50, 50); // set the frame where you want to see the background color or background image to be visible in your button

  buttonlayer.backgroundColor=[[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourimage.png"] ] CGColor]; // if u want image (but remember the image size must same as your layer frame it may  look bad if the image bounds does not mach your layer bounds, because you are setting color not image)


  // to set color (donot use if you want image, use colorWithPatternImage to set image color)
   buttonlayer.backgroundColor=[[UIColor graycolor] CGColor];  // if you want only color

    // you can add round rects to the button layer

    buttonlayer.cornerRadius=2.5f;

   [yourbutton.layer addSublayer:buttonlayer];

我不确定你在找什么我希望这对你有帮助但检查一下图像enter image description here

关于ios - xcode iOS 增加UIButton点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225443/

相关文章:

ios - Xcode IB - UIButton 上可调整大小的图像

ios - 如何在运行时获取 iOS 设备 CPU 架构

ios - 多级UIView(xib类型)不触发UITapGestureRecognizer

arrays - Swift - 无法为索引类型为 NSNumber 的值类型 [String] 下标?

crash - Xcode在界面生成器中意外退出

ios - XCode 6.1 (6A1052d) 上未显示 IBOutlets

ios - Xcode 6 + 具有多个 View 的 Storyboard + 处理键盘事件

ios - 为什么 searchBar "textDidChange"获取服务器请求并出错并崩溃

ios - Swift - 打开内置的 iOS 字典来查找词义

ios - ICECAST 数据的 NSRegularExpression 模式