简体   繁体   English

自动布局多个UITextviews和标签-Swift

[英]Auto Layout Multiple UITextviews and labels - Swift

I am creating a iOS application for a student registration. 我正在为学生注册创建一个iOS应用程序。 My problem is I cannot get the auto layout to work for all the devices. 我的问题是我无法使自动版式适用于所有设备。 I want to layout my interface with even spacing between each label and textview. 我想布局我的界面,使每个标签和textview之间的间距均匀。 Any help will be much appreciated. 任何帮助都感激不尽。 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
    }
}

I am attaching a screenshot with constraints so that you can basic idea how to put proper constraints, but in your case it would be better to go with table view if your number of fields increases then all your fields would come automatically inside scrollView and it would be easliy visible in smaller devices, But you can have look into this screenshot if you don't want to use tableview and have less number of fields. 我附有一个带有约束的屏幕截图,以便您可以基本了解如何设置适当的约束,但是如果您的字段数量增加,则最好使用表格视图,那么所有字段将自动出现在scrollView中,并且在较小的设备上很容易看到,但是如果您不想使用tableview并且字段数较少,则可以查看此屏幕截图。 在此处输入图片说明

A UITableViewController would be the best approach. UITableViewController将是最好的方法。

Just keep in mind that UIStackView provides a streamlined way to automatically lay out a collection of views. 请记住, UIStackView提供了一种简化的方式来自动布置视图集合。 It handles the alignment and spacing for you: 它为您处理对齐和间距:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM