-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIView+AutoLayout.swift
44 lines (32 loc) · 1.34 KB
/
UIView+AutoLayout.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
//
// UIView+AutoLayout.swift
// ScalingHeaderImageViewSample
import UIKit
extension UIView {
// MARK: - internal methods
func addAutoLayoutedSubview(_ view: UIView) {
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
}
// MARK: - NSLayoutConstraint / [NSLayoutConstraint]
func fillConstraints() -> [NSLayoutConstraint] {
guard let superview = self.superview else {
assertionFailure("Failed to get a superview")
return []
}
return [leadingAnchor.constraint(equalTo: superview.leadingAnchor),
topAnchor.constraint(equalTo: superview.topAnchor),
trailingAnchor.constraint(equalTo: superview.trailingAnchor),
bottomAnchor.constraint(equalTo: superview.bottomAnchor)]
}
func fillConstraintsWithTopSafeArea() -> [NSLayoutConstraint] {
guard let superview = self.superview else {
assertionFailure("Failed to get a superview")
return []
}
return [leadingAnchor.constraint(equalTo: superview.leadingAnchor),
topAnchor.constraint(equalTo: superview.safeAreaLayoutGuide.topAnchor),
trailingAnchor.constraint(equalTo: superview.trailingAnchor),
bottomAnchor.constraint(equalTo: superview.bottomAnchor)]
}
}