This is my own Auto layout functions. I tried some libs before and found TinyConstraint from Robert-Hein Hooijmans (https://github.com/roberthein/TinyConstraints). It's really exciting and sweet. Learnt from his code and tried my own one. Now, here it is.
- Short syntax
- Easily and quickly understand
Fill a view to other view
orangeView.topAnchor.constraint(equalTo: view.topAnchor, constant: 68).isActive = true orangeView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 68).isActive = true orangeView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -68).isActive = true orangeView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -68).isActive = true
orangeView.fill(toView: view, space: UIEdgeInsets(space: 68))
orangeView.horizontal(toView: view, space: 68) orangeView.vertical(toView: view, space: 68)
Align a view to left/right/top/bottom to other view
yellowView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8) yellowView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8)
yellowView.bottom(toView: view, space: -8) yellowView.right(toView: view, space: -8)
yellowView.bottomLeft(toView: view, bottom: -8, left: -8)
redView.widthAnchor.constraint(equalToConstant: 100).isActive = true redView.heightAnchor.constraint(equalToConstant: 100).isActive = true
redView.size(CGSize(width: 100, height: 100))
Stack views but don't want to use UIStackView, try this
stackHorizontally(views: [orangeView, redView], viewSpaces: 4, leftSpace: 16, rightSpace: nil)
rightSpace is nil that means the
redView's rightAnchor is free (not set to the superview)
leftAnchor align to
superview's leftAnchor with 16px spacing.
Suggestions or feedback?
Feel free to create a pull request, open an issue