-
iOS ) Storyboard없이 레이아웃 꾸밀때 constraint 작업 편하게 해주는 메서드Programing Language/iOS(Swift) 2019. 11. 15. 14:33728x90반응형
1. 코드
extension UIView { func anchor(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, paddingTop: CGFloat? = 0, paddingLeft: CGFloat? = 0, paddingBottom: CGFloat? = 0, paddingRight: CGFloat? = 0, width: CGFloat? = nil, height: CGFloat? = nil) { translatesAutoresizingMaskIntoConstraints = false if let top = top { topAnchor.constraint(equalTo: top, constant: paddingTop!).isActive = true } if let left = left { leftAnchor.constraint(equalTo: left, constant: paddingLeft!).isActive = true } if let bottom = bottom { if let paddingBottom = paddingBottom { bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true } } if let right = right { if let paddingRight = paddingRight { rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true } } if let width = width { widthAnchor.constraint(equalToConstant: width).isActive = true } if let height = height { heightAnchor.constraint(equalToConstant: height).isActive = true } } }
2. 사용 예시
UIViewObject.anchor(top: wirtetextField.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor,paddingTop: 20, paddingLeft: 10, paddingRight: 10, height: 40)
728x90반응형'Programing Language > iOS(Swift)' 카테고리의 다른 글
iOS ) MARK 주석 달기 (0) 2019.11.22 iOS ) 특정위치(좌표)에 동그라미 그리기 (점찍기) (0) 2019.11.21 iOS) Custom Camera에서 view 터치 할때 사각형 이미지 표시하기 (0) 2019.11.14 iOS) Custom Camera 화면 전환 (0) 2019.11.14 iOS) 멀티쓰레딩 - GCD, DispatchQueue (0) 2019.11.13