-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathCardWithImageViewAndButtonsExample.swift
112 lines (98 loc) · 3.67 KB
/
CardWithImageViewAndButtonsExample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2020-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import UIKit
import MaterialComponents.MaterialButtons_ButtonThemer
import MaterialComponents.MaterialButtons_Theming
import MaterialComponents.MaterialCards_Theming
import MaterialComponents.MaterialContainerScheme
class CustomCard: MDCCard {
static let cardWidth: CGFloat = 300
let imageView: UIImageView = UIImageView()
let cardButton1: MDCButton = MDCButton()
let cardButton2: MDCButton = MDCButton()
override func layoutSubviews() {
super.layoutSubviews()
if imageView.superview == nil { addSubview(imageView) }
if cardButton1.superview == nil { addSubview(cardButton1) }
if cardButton2.superview == nil { addSubview(cardButton2) }
cardButton1.sizeToFit()
cardButton2.sizeToFit()
imageView.frame = CGRect(
x: 0,
y: 0,
width: CustomCard.cardWidth,
height: 200)
cardButton1.frame = CGRect(
x: 8,
y: imageView.frame.maxY + 8,
width: cardButton1.frame.width,
height: cardButton1.frame.height)
cardButton2.frame = CGRect(
x: 8,
y: cardButton1.frame.maxY + 8,
width: cardButton2.frame.width,
height: cardButton2.frame.height)
}
override var intrinsicContentSize: CGSize {
return CGSize(width: CustomCard.cardWidth, height: cardButton2.frame.maxY + 8)
}
}
class CardWithImageViewAndButtonsExample: UIViewController {
let card: CustomCard = CustomCard()
@objc var containerScheme: MDCContainerScheming
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
containerScheme = MDCContainerScheme()
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = containerScheme.colorScheme.backgroundColor
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setUpCard()
}
func setUpCard() {
let bundle = Bundle(for: CardWithImageViewAndButtonsExample.self)
card.imageView.image = UIImage(named: "sample-image", in: bundle, compatibleWith: nil)
card.cardButton1.setTitle("Action 1", for: .normal)
card.cardButton2.setTitle("Action 2", for: .normal)
card.cardButton1.applyTextTheme(withScheme: containerScheme)
card.cardButton2.applyTextTheme(withScheme: containerScheme)
card.cornerRadius = 8
card.applyTheme(withScheme: containerScheme)
card.setNeedsLayout()
card.layoutIfNeeded()
card.frame = CGRect(
x: card.frame.minX,
y: card.frame.minY,
width: card.intrinsicContentSize.width,
height: card.intrinsicContentSize.height)
if card.superview == nil { view.addSubview(card) }
card.center = view.center
}
}
extension CardWithImageViewAndButtonsExample {
@objc class func catalogMetadata() -> [String: Any] {
return [
"breadcrumbs": ["Cards", "Card README example"],
"description": "Cards contain content and actions about a single subject.",
"primaryDemo": true,
"presentable": true,
]
}
}