0
Swift 3을 사용하여 프로그래밍 방식으로 양식을 작성하려고합니다. 앱을 실행할 때 올바른 제약 조건이 적용되지 않습니다.iOS Swift 3/XCode 8 오른쪽 제약 조건이 적용되지 않습니다.
내가 가지고있는 UIScollView
은 UITextField
을 포함하며, 아래의 미리보기에서 볼 수 있듯이 ScrollView는 빨간색이고 TextField는 흰색입니다.
// View Controller Properties
var scrollView: UIScrollView!
var firstName: UITextField!
// Inside viewDidLoad()
self.scrollView = UIScrollView()
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.backgroundColor = UIColor.red
self.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
self.view.addSubview(self.scrollView)
let top = NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0)
let left = NSLayoutConstraint(item: self.scrollView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0)
let right = NSLayoutConstraint(item: self.scrollView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: self.scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0)
self.view.addConstraints([top, left, right, bottom])
self.firstName = UITextField()
self.firstName.translatesAutoresizingMaskIntoConstraints = false
self.firstName.backgroundColor = UIColor.white
self.firstName.placeholder = "First Name"
self.scrollView.addSubview(self.firstName)
let top = NSLayoutConstraint(item: self.firstName, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1, constant: 20)
let left = NSLayoutConstraint(item: self.firstName, attribute: .left, relatedBy: .equal, toItem: self.scrollView, attribute: .left, multiplier: 1, constant: 20)
let right = NSLayoutConstraint(item: self.firstName, attribute: .right, relatedBy: .equal, toItem: self.scrollView, attribute: .right, multiplier: 1, constant: 0)
let height = NSLayoutConstraint(item: self.firstName, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40)
self.scrollView.addConstraints([top, right, left, height])
내가 대신
.trailing
및
.trailingMargin
을 사용하지만 아무것도 작동하지 않습니다했습니다. 어떤 제안이라도 대단히 감사하겠습니다.
let margins = view.layoutMarginsGuide
view.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 20).isActive = true
참고 : 아이폰 OS 9의 +
당신이 코드를 사용할 수 있습니다