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

时间:2016-01-12 03:09:01

标签: ios swift ios-autolayout

我正在为学生注册创建一个iOS应用程序。我的问题是我无法使自动布局适用于所有设备。我想在每个标签和textview之间使用均匀的间距来布局我的界面。任何帮助都感激不尽。 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
    }
}

2 个答案:

答案 0 :(得分:0)

我附加了一个带有约束的屏幕截图,以便您可以基本了解如何设置适当的约束,但在您的情况下,如果您的字段数增加,那么最好使用表视图,那么所有字段都会自动出现在scrollView中并且它在较小的设备中很容易看到,但如果您不想使用tableview并且字段数较少,则可以查看此屏幕截图。enter image description here

答案 1 :(得分:0)

UITableViewController是最好的方法。

请记住,UIStackView提供了一种自动布局视图集合的简化方法。它为您处理对齐和间距:

enter image description here