【发布时间】:2013-10-09 04:21:19
【问题描述】:
iOS App Store 有一个蓝色圆形框架按钮,用于购买/下载应用程序。
在我的应用程序中,您可以下载其他内容,我希望有一个类似的按钮,只是因为它对用户来说看起来很熟悉。
如果你不知道,我的意思是:这些按钮,比如“$3.99”
这怎么可能?
【问题讨论】:
标签: ios objective-c cocoa-touch uibutton
iOS App Store 有一个蓝色圆形框架按钮,用于购买/下载应用程序。
在我的应用程序中,您可以下载其他内容,我希望有一个类似的按钮,只是因为它对用户来说看起来很熟悉。
如果你不知道,我的意思是:这些按钮,比如“$3.99”
这怎么可能?
【问题讨论】:
标签: ios objective-c cocoa-touch uibutton
在 Swift 5.1 / iOS 13 中,您可以创建 UIButton 的子类,以便拥有一个看起来像 iOS AppStore 应用程序中的蓝色圆角边框按钮的自定义按钮。
以下代码显示了如何正确管理色调颜色(当按钮出现在 UIAlertController 的暗视图后面时)、标题颜色、突出显示的背景颜色、边框样式、边框颜色和内容插入.
CustomButton.swift:
import UIKit
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setProperties()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setProperties()
}
func setProperties() {
// Set the layer's properties
layer.borderColor = tintColor?.cgColor
layer.borderWidth = 1
layer.cornerRadius = 4
// Set colors for title's states
setTitleColor(tintColor, for: .normal)
setTitleColor(.white, for: .highlighted)
// Add some margins between the title (content) and the border
contentEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
}
override var isHighlighted: Bool {
didSet {
// Toggle the background color according to button's highlighted state
backgroundColor = super.isHighlighted ? tintColor : nil
}
}
override func tintColorDidChange() {
super.tintColorDidChange()
// When the tint color is changed by the system (e.g. when the button appears below the dimmed view of a UIAlertController), we have to manually update border color and title's text color
layer.borderColor = tintColor?.cgColor
setTitleColor(tintColor, for: .normal)
}
}
ViewController.swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondarySystemBackground
let button = CustomButton()
button.setTitle("Normal", for: .normal)
button.setTitle("Highlighted", for: .highlighted)
button.addTarget(self, action: #selector(presentAlert(_:)), for: .touchUpInside)
view.addSubview(button)
// auto layout
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
}
/// Present alert when button is tapped
@objc func presentAlert(_ sender: UIButton) {
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil)
}
}
下图显示了自定义按钮在正常状态下的显示方式,当系统 tinColor 更改时(在 UIAlertController 的暗色视图后面)并处于 highlighted 状态。
【讨论】:
对于标准的 iOS 控件元素,例如 UIButton、UILabel、
你应该使用 UIView tintColor 属性:
buyButton.layer.borderColor = buyButton.tintColor.CGColor;
【讨论】:
对于 Duncan C 对 abbood 答案的扩展的 swift 版本:
extension CALayer {
var borderUIColor: UIColor? {
get {
if let borderColor = borderColor {
return UIColor(CGColor: borderColor)
}
return nil
}
set {
borderColor = newValue?.CGColor ?? nil
}
}
}
【讨论】:
为了扩展@abbood 的出色答案,实际上可以从 IB 设置视图层的边框颜色和背景颜色,但需要做一些准备工作。
我在 NSView 上创建了一个自定义类别,CALayer+setUIColor.h。
设置边框颜色只有一个方法,setBorderUIColor。它将 UIColor 作为输入,获取 UIColor 的底层 NSColor,并将该颜色应用于视图层。
然后,在 IB 中,您只需添加一个用户定义的运行时属性,如下所示:
键路径 layer.borderUIColor 类型 颜色 价值 想要的颜色。
在运行时,系统调用您的方法,传入 IB 中定义的 UIColor。类别从 UIColor 中获取 CGColor 并将其应用于图层。
您可以在我的一个名为
的 github 项目中的一个工作项目中看到这一点我也为设置图层的背景颜色属性做了同样的事情,但在上面的项目中没有。
【讨论】:
【讨论】:
对于您描述的简单边框,请使用 Dima 的答案,使用 CALAyer。
如果您想要更多,例如带有渐变的圆角矩形,则使用此方法作为基础:
创建一个自定义视图,该视图绘制一个圆角矩形并将其放置在按钮上。 使用此处的搜索功能搜索绘制圆角矩形。 该绘图通过绘制 4 条具有定义半径(角)的圆弧和 4 条直线来进行。
FTR,根据 Alex 和 Brian,这是制作具有正确 iOS7 圆角的 UIView 的方法。
我很确定 CGPathCreateWithRoundedRect 不会为您提供“新”圆角。您必须使用 bezierPathWithRoundedRect 作为“新”角。
#import "UIViewWithIOS7RoundedCorners.h"
@implementation UIViewWithIOS7RoundedCorners
-(void)drawRect:(CGRect)rect
{
// for a filled shape...
UIBezierPath* path =
[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:4];
[[UIColor blueColor] setFill];
[path fill];
// for just a border...
int thickness=2;
UIBezierPath* path =
[UIBezierPath bezierPathWithRoundedRect:
CGRectInset(self.bounds, thickness/2, thickness/2)
cornerRadius: 4];
path.lineWidth = thickness;
[[UIColor blueColor] setStroke];
[path stroke];
}
@end
// ps don't forget to set .backgroundColor=[UIColor clearColor]
// perhaps in initWithCoder/initWithFrame
希望对某人有所帮助
【讨论】:
+[UIBezierPath bezierPathWithRoundedRect:cornerRadius:] 使用新的圆角半径样式。此外,您可以为所有按钮生成单个图像,从而提高 GPU 渲染性能。
您可以非常轻松地操作按钮的 CALayer。
// assuming you have a UIButton or more generally a UIView called buyButton
buyButton.layer.cornerRadius = 2;
buyButton.layer.borderWidth = 1;
buyButton.layer.borderColor = [UIColor blueColor].CGColor;
// (note - may prefer to use the tintColor of the control)
您可以调整其中的每一个以获得所需的颜色和边框效果。
您还必须在要使用 CALayers 的任何文件中添加导入
#import <QuartzCore/QuartzCore.h>
【讨论】:
【讨论】: