iOS应用商店有一个蓝色圆框框的按钮,用于购买/下载应用程序。
在我的应用中,您可以下载额外的内容,我想要一个类似的按钮,因为它看起来很熟悉。
如果您不知道我的意思:这些按钮就像“$3.99”
这怎么可能?
iOS应用商店有一个蓝色圆框框的按钮,用于购买/下载应用程序。
在我的应用中,您可以下载额外的内容,我想要一个类似的按钮,因为它看起来很熟悉。
如果您不知道我的意思:这些按钮就像“$3.99”
这怎么可能?
// 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>
对于标准的iOS控件元素,例如UIButton
,UILabel
,
您应该使用UIView
的 tintColor
属性:
buyButton.layer.borderColor = buyButton.tintColor.CGColor;
对于像您描述的简单边框,请使用Dima使用CALAyer的答案。
如果你想要更多,比如有渐变效果的圆角矩形,则可以使用以下方法:
创建一个自定义视图,用于绘制圆角矩形,并将其放置在按钮上。可以使用此处的搜索功能来搜索绘制圆角矩形的方法。绘制方法是通过绘制4个具有定义半径(角)的弧和4条直线来完成的。
顺便说一句,这里是如何制作具有正确iOS7圆角的UIView,Alex和Brian提供了解决方法。
我非常确定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渲染性能。 - Brian Nickel对于 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
}
}
}
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)
}
}
UIAlertController
有关。