StackView框架未设置为Window的边界

时间:2016-04-14 15:18:51

标签: ios swift uistackview

我在stackview中有一些文本字段和一个按钮,我希望它们填满整个屏幕宽度。但是,我似乎无法使stackview的宽度等于屏幕的宽度。我尝试将帧设置为superview.boundsself.view.frame,但这两种方法都没有。它仍然只是部分填满了屏幕。我觉得我在某处限制了大小,但我不知道在哪里。感谢您的帮助!

代码: exitButton = UIButton(frame:CGRect(origin:CGPointZero,size:CGSize(width:self.view.frame.width,height:75)))         exitButton.setTitle(" X",forState:.Normal)         exitButton.tintColor = UIColor.grayColor()         exitButton.translatesAutoresizingMaskIntoConstraints = false         exitButton.addTarget(self,action:" exitPage",forControlEvents:.TouchUpInside)

    firstNameText = UITextField(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 100)))
    firstNameText.borderStyle = .Line
    firstNameText.tag = 0
    firstNameText.font = UIFont.systemFontOfSize(30)
    firstNameText.returnKeyType = UIReturnKeyType.Done
    firstNameText.text = placeholders[firstNameText.tag]
    firstNameText.textColor = UIColor.lightGrayColor()
    firstNameText.delegate = self
    firstNameText.translatesAutoresizingMaskIntoConstraints = false

    lastNameText = UITextField(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 100)))
    lastNameText.borderStyle = .Line
    lastNameText.tag = 1
    lastNameText.font = UIFont.systemFontOfSize(30)
    lastNameText.returnKeyType = UIReturnKeyType.Done
    lastNameText.text = placeholders[lastNameText.tag]
    lastNameText.textColor = UIColor.lightGrayColor()
    lastNameText.delegate = self
    lastNameText.translatesAutoresizingMaskIntoConstraints = false

    emailText = UITextField(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 100)))
    emailText.borderStyle = .Line
    emailText.tag = 2
    emailText.font = UIFont.systemFontOfSize(30)
    emailText.returnKeyType = UIReturnKeyType.Done
    emailText.text = placeholders[emailText.tag]
    emailText.textColor = UIColor.lightGrayColor()
    emailText.delegate = self
    emailText.translatesAutoresizingMaskIntoConstraints = false

    passText = UITextField(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 100)))
    passText.borderStyle = .Line
    passText.tag = 3
    passText.font = UIFont.systemFontOfSize(30)
    passText.returnKeyType = UIReturnKeyType.Done
    passText.text = placeholders[passText.tag]
    passText.textColor = UIColor.lightGrayColor()
    passText.delegate = self
    passText.translatesAutoresizingMaskIntoConstraints = false

    retypePassText = UITextField(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 100)))
    retypePassText.borderStyle = .Line
    retypePassText.tag = 4
    retypePassText.font = UIFont.systemFontOfSize(30)
    retypePassText.returnKeyType = UIReturnKeyType.Done
    retypePassText.text = placeholders[retypePassText.tag]
    retypePassText.textColor = UIColor.lightGrayColor()
    retypePassText.delegate = self
    retypePassText.translatesAutoresizingMaskIntoConstraints = false

    signUpButton = UIButton(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 75)))
    signUpButton.setTitle("Sign Up", forState: .Normal)
    signUpButton.backgroundColor = UIColor(red: 34/255, green: 139/255, blue: 34/255, alpha: 1.0)
    signUpButton.translatesAutoresizingMaskIntoConstraints = false
    signUpButton.addTarget(self, action: "signUpAction", forControlEvents: .TouchUpInside)

    let screenStackView = UIStackView()
    screenStackView.addArrangedSubview(firstNameText)
    screenStackView.addArrangedSubview(lastNameText)
    screenStackView.addArrangedSubview(passText)
    screenStackView.addArrangedSubview(retypePassText)

    let fillerView = UIView()
    fillerView.translatesAutoresizingMaskIntoConstraints = false
    fillerView.heightAnchor.constraintEqualToConstant(20).active = true
    screenStackView.addArrangedSubview(fillerView)
    screenStackView.addArrangedSubview(signUpButton)
    screenStackView.alignment = .Fill
    screenStackView.axis = .Vertical
    screenStackView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(screenStackView)
    screenStackView.frame = self.view.bounds
    print("\(screenStackView.frame.width), \(self.view.bounds.width)")
    let centerXConstraint = NSLayoutConstraint(item: screenStackView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0)
    let centerYConstraint = NSLayoutConstraint(item: screenStackView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: -75)
    self.view.addConstraint(centerXConstraint)
    self.view.addConstraint(centerYConstraint)

    self.view.addSubview(exitButton)
    exitButton.topAnchor.constraintEqualToAnchor(self.view.topAnchor, constant: 50).active = true
    exitButton.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor, constant: -40).active = true

0 个答案:

没有答案