Skip to content

Commit 05f5c9f

Browse files
committed
another useful utility
1 parent 976faf0 commit 05f5c9f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

bk2ch28AppendixB/bk2ch40AppendixB/ViewController.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ extension UIView {
146146
}
147147

148148
// not in the book, but maybe they should be:
149-
// utilities for understandind an untouchable view
149+
// utilities for understanding an untouchable view
150150

151151
extension UIView {
152152
@objc func reportNoninteractiveSuperview() {
@@ -174,6 +174,21 @@ extension UIView {
174174
}
175175
}
176176

177+
// utility for shortening constraint creation
178+
// I'm not big on this kind of thing, but this is so common it seems to need something
179+
180+
extension UIView {
181+
func pinToSuperview(_ insets:NSDirectionalEdgeInsets = .zero) {
182+
guard let sup = self.superview else { return }
183+
self.translatesAutoresizingMaskIntoConstraints = false
184+
self.topAnchor.constraint(equalTo: sup.topAnchor, constant: insets.top).isActive = true
185+
self.trailingAnchor.constraint(equalTo: sup.trailingAnchor, constant: -insets.trailing).isActive = true
186+
self.leadingAnchor.constraint(equalTo: sup.leadingAnchor, constant: insets.leading).isActive = true
187+
self.bottomAnchor.constraint(equalTo: sup.bottomAnchor, constant: -insets.bottom).isActive = true
188+
}
189+
}
190+
191+
177192

178193
extension UIControl {
179194
func addAction(for event: UIControl.Event,

0 commit comments

Comments
 (0)