自动布局多个 UITextview 和标签 - Swift
Auto Layout Multiple UITextviews and labels - Swift
我正在创建一个 iOS 学生注册申请。我的问题是我无法让自动布局适用于所有设备。我想布局我的界面,每个标签和文本视图之间的间距均匀。任何帮助都感激不尽。 storyboard capture here
@IBOutlet weak var bottomconst: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
//
// NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func keyboardWasShown(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
//bottomspace = self.bottomconst.constant;
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.layoutIfNeeded()
self.bottomconst.constant = keyboardFrame.size.height + 20
})
}
func keyboardWillHide(sender: NSNotification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.bottomconst?.constant -= keyboardSize.height
}
}
我附上了一张带有约束的截图,这样你就可以基本了解如何设置适当的约束,但在你的情况下,如果你的字段数量增加,那么你所有的字段数量增加,那么最好使用 table 查看字段会自动出现在 scrollView 中,并且在较小的设备中很容易看到,但是如果您不想使用 tableview 并且字段数量较少,您可以查看此屏幕截图。
A UITableViewController
将是最好的方法。
请记住,UIStackView
提供了一种自动布置视图集合的简化方法。它为您处理对齐和间距:
我正在创建一个 iOS 学生注册申请。我的问题是我无法让自动布局适用于所有设备。我想布局我的界面,每个标签和文本视图之间的间距均匀。任何帮助都感激不尽。 storyboard capture here
@IBOutlet weak var bottomconst: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
//
// NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func keyboardWasShown(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
//bottomspace = self.bottomconst.constant;
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.layoutIfNeeded()
self.bottomconst.constant = keyboardFrame.size.height + 20
})
}
func keyboardWillHide(sender: NSNotification) {
if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.bottomconst?.constant -= keyboardSize.height
}
}
我附上了一张带有约束的截图,这样你就可以基本了解如何设置适当的约束,但在你的情况下,如果你的字段数量增加,那么你所有的字段数量增加,那么最好使用 table 查看字段会自动出现在 scrollView 中,并且在较小的设备中很容易看到,但是如果您不想使用 tableview 并且字段数量较少,您可以查看此屏幕截图。
A UITableViewController
将是最好的方法。
请记住,UIStackView
提供了一种自动布置视图集合的简化方法。它为您处理对齐和间距: