UIButton 删除边框与触摸覆盖

Posted

技术标签:

【中文标题】UIButton 删除边框与触摸覆盖【英文标题】:UIButton remove border with overlay on touch 【发布时间】:2012-11-21 17:57:41 【问题描述】:

我需要一个 UIButton,但我不想要它的边框。

但是,当有人触摸按钮时,我仍然希望保持漂亮的渐变叠加

就像在 Facebook iPad 应用程序上一样,它们在动态消息中的文本链接在您触摸它们时会显示一个灰色框覆盖。

我在这里看到了一个关于使用 UISegmentedControl 的解决方案。但是我不想在解决方案中使用渐变/Quartz 框架。

以前有人遇到过这个问题吗?

更新

这是一个插图

(1) 是在您触摸之前 (2) 是当您触摸按钮时发生的情况。

我基本上只是想复制这种效果,我猜是透明的“覆盖”,就像你触摸按钮时发生的那样,但没有边框。

【问题讨论】:

也许包括我们这些不使用 Facebook 的人的屏幕截图? [link]tinypic.com/r/160tmo/6 (1) 在您触摸之前 (2) 是在您触摸链接时发生的情况。我基本上只是想复制这种效果,我猜是透明的“覆盖”,就像你触摸按钮时发生的那样,但没有边框 请不要在与 Xcode 无关的问题上使用xcode 标签。 【参考方案1】:

使用以下内容:

UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
[yourButton setFrame:CGRectMake(0, 0, 200, 44)];
[yourButton setTitle:@"like" forState:UIControlStateNormal];

[yourButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[yourButton setBackgroundImage:[UIImage imageNamed:@"gradient.png"] forState:UIControlStateHighlighted];

// Round corners? Requires Quartzcore framework
[yourButton.layer setCornerRadius:8.0f];
[yourButton.layer setMasksToBounds:YES];
[yourButton.layer setBorderWidth:0.0f];

【讨论】:

【参考方案2】:

制作一个你想要的颜色的 UIImage,并调用setBackgroundImage:forState: 获得高亮状态(当用户点击按钮时,按钮高亮);

【讨论】:

以上是关于UIButton 删除边框与触摸覆盖的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式为处于触摸状态的 UIButton 创建灰色叠加层?

iPhone iOS自定义UIButton与CAGradientLayer,如何让它指示触摸?

子类化 UIButton 并覆盖触摸事件 - 不起作用

UIButton 内的 UIImageView 具有默认填充 - 如何删除?

在 UIButton 上实现触摸取消

iOS - 如何删除 UIButton 边框颜色?