Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension UIView{
- func align(_ anchor : Anchor, to siblingView: UIView) -> UIView{
- switch anchor {
- case .leading:
- self.frame.origin.x = siblingView.frame.origin.x
- case .trailing:
- self.frame.origin.x = siblingView.frame.origin.x + siblingView.frame.width - self.frame.width
- case .top:
- self.frame.origin.y = siblingView.frame.origin.y
- case .botton:
- self.frame.origin.y = siblingView.frame.origin.y + siblingView.frame.height - self.frame.height
- case .centerX:
- self.center.x = siblingView.center.x
- case .centerY:
- self.center.y = siblingView.center.y
- }
- return self
- }
- }
- enum Anchor {
- case leading
- case top
- case botton
- case centerX
- case centerY
- case trailing
- }
- /*
- - align view if they are not at same view hierarchy。
- > 总体思路就是 要移动那个, 就把另一个的坐标系变成这个要动的
- ```swift
- */
- extension UIView{
- func alignWithDifferentCoordinate(_ anchor : Anchor, to siblingView: UIView) -> UIView{
- //move self, so convert siblingView's frame to self's coordinate
- let rect = siblingView.convert(siblingView.bounds, to: self.superview) //-> 这里siblingView bound 就是siblingView自己把自己当坐标系
- switch anchor {
- case .leading:
- self.frame.origin.x = rect.origin.x
- case .trailing:
- self.frame.origin.x = rect.origin.x + rect.width - self.frame.width
- case .top:
- self.frame.origin.y = rect.origin.y
- case .botton:
- self.frame.origin.y = rect.origin.y + rect.height - self.frame.height
- case .centerX:
- self.center.x = rect.origin.x + rect.width / 2
- case .centerY:
- self.center.y = rect.origin.y + rect.height / 2
- }
- return self
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement