Skip to content

Instantly share code, notes, and snippets.

@nurtugan
Last active August 7, 2020 15:22
Show Gist options
  • Save nurtugan/2a38899070065cc628fa9585cf562088 to your computer and use it in GitHub Desktop.
Save nurtugan/2a38899070065cc628fa9585cf562088 to your computer and use it in GitHub Desktop.
Useful code snippets from Auto Layout by Tutorials book
// Chapter 1: Introducing Auto Layout
// Chapter 2: Construct Auto Layout with the Interface Builder
// 1
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
view.addSubview(contactPreviewView)
contactPreviewView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ // this line of code more performant than activating each constraint individually
contactPreviewView.widthAnchor.constraint(equalToConstant: 150),
contactPreviewView.heightAnchor.constraint(equalToConstant: 150),
contactPreviewView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
contactPreviewView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
contactPreviewView.transform = .init(scaleX: 1.25, y: 1.25)
contactPreviewView.alpha = 0
UIView.animate(withDuration: 0.3) {
self.contactPreviewView.alpha = 1
self.contactPreviewView.transform = .identity
}
}
// 2
@objc
private func hideContactPreview() {
UIView.animate(withDuration: 0.3, animations: {
self.contactPreviewView.transform = .init(scaleX: 1.25, y: 1.25)
self.contactPreviewView.alpha = 0
}, completion: { _ in self.contactPreviewView.removeFromSuperview() })
}
// 3
// Setting up class to "File's Owner"
// 4
private func loadView() {
let bundle = Bundle(for: ContactPreviewView.self)
let nib = UINib(nibName: "ContactPreviewView", bundle: bundle)
let view = nib.instantiate(withOwner: self).first as! UIView
view.frame = bounds
addSubview(view)
}
// Chapter 4: Construct Auto Layout with Code
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(greaterThanOrEqualTo: leadingAnchor, constant: 20), // 1.0 giving range of constraint
stackView.leadingAnchor.constraint(lessThanOrEqualTo: leadingAnchor, constant: 500), // 1.1 giving range of constraint
profileImageView.widthAnchor.constraint(equalToConstant: 120), // 2.0 configuring aspect ratio 1:1
profileImageView.widthAnchor.constraint(equalTo: profileImageView.heightAnchor), // 2.1 configuring aspect ratio 1:1
leftSpacerView.widthAnchor.constraint(equalTo: rightSpacerView.widthAnchor) // 3.0 giving equal width for two views
])
// Chapter 5: Scroll View
// Scroll view has two layout guides
// 1. scrollView.contentLayoutGuide
// 2. scrollView.frameLayoutGuide
private func setupMainStackView() {
mainStackView.axis = .vertical
mainStackView.distribution = .equalSpacing
mainStackView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(mainStackView)
let contentLayoutGuide = scrollView.contentLayoutGuide
NSLayoutConstraint.activate([
mainStackView.widthAnchor.constraint(equalTo: view.widthAnchor),
mainStackView.leadingAnchor.constraint(equalTo: contentLayoutGuide.leadingAnchor),
mainStackView.trailingAnchor.constraint(equalTo: contentLayoutGuide.trailingAnchor),
mainStackView.topAnchor.constraint(equalTo: contentLayoutGuide.topAnchor),
mainStackView.bottomAnchor.constraint(equalTo: contentLayoutGuide.bottomAnchor)
])
}
let insets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 20)
let image = UIImage.init(named: greenBubbleImageName)?.imageFlippedForRightToLeftLayoutDirection()
bubbleImageView.image = image?.resizableImage(withCapInsets: insets, resizingMode: .stretch)
// Chapter 6: Self-Sizing Views
override func layoutSubviews() {
super.layoutSubviews()
let height = collectionView.frame.height - verticalInset * 2
let width = height
let itemSize = CGSize(width: width, height: height)
flowLayout.itemSize = itemSize
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment