这是我第一次在Stack Overflow上发布内容。 我只是从一个应用程序开始,用Swift编写,没有故事板,只有SwiftUI。 在我的第一页上,我希望用户登录-用户名,电子邮件,密码。 这三个文本字段在较小的视图中在一起。 这是问题所在:我希望它们之间使用分隔符:
我已经完成了所有代码,但似乎没有用。我找不到解决方案,为什么不显示它们。如果已经有解决方法,请保留下面的链接。
import Foundation
import UIKit
// Login Window
class LoginController : UIViewController {
//MARK: create Elemnts
//Title - Chaty
let titleLabel : UILabel = {
let label = UILabel()
label.text = "ChatY"
label.font = UIFont(name: "Kefa", size: 44)
label.textAlignment = .center
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
//Profil Image
let profilImageView : UIImageView = {
let image = UIImageView()
image.image = UIImage(systemName: "person.crop.circle.badge.plus")?.withRenderingMode(.alwaysOriginal) .withTintColor(.white)
image.contentMode = .scaleAspectFill
image.layer.masksToBounds = false
image.frame.size.width = 80
image.frame.size.height = 80
image.alpha = 1.0
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()
let containerView : UIView = {
let uiView = UIView()
uiView.backgroundColor = UIColor.white
uiView.layer.cornerRadius = 5
uiView.layer.masksToBounds = true
uiView.translatesAutoresizingMaskIntoConstraints = false
return uiView
}()
let nameTextField : UITextField = {
let textField = UITextField()
textField.placeholder = "Username"
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
let nameSeperator : UIView = {
let seperator = UIView()
seperator.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
return seperator
}()
let emailTextField : UITextField = {
let textfield = UITextField()
textfield.placeholder = "E-Mail"
textfield.translatesAutoresizingMaskIntoConstraints = false
textfield.keyboardType = .emailAddress
return textfield
}()
let eMailSeperator : UIView = {
let seperator = UIView()
seperator.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
return seperator
}()
let passwordTextField : UITextField = {
let textfield = UITextField()
textfield.placeholder = "Password"
textfield.translatesAutoresizingMaskIntoConstraints = false
textfield.isSecureTextEntry = true
return textfield
}()
//MARK: ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.brown
view.addSubview(titleLabel)
view.addSubview(profilImageView)
view.addSubview(containerView)
setupTitleLabel()
setupProfilImageView()
setupElementsInContainerView()
}
//MARK: Setup Elementspositon
func setupTitleLabel() {
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
titleLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
}
func setupProfilImageView() {
profilImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
profilImageView.bottomAnchor.constraint(equalTo: containerView.topAnchor, constant: -100).isActive = true
profilImageView.widthAnchor.constraint(equalToConstant: 90).isActive = true
}
func setupElementsInContainerView() {
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true // Mid x
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true //Mid y
containerView.widthAnchor.constraint(equalTo: view.widthAnchor , constant: -30).isActive = true //With
containerView.heightAnchor.constraint(equalToConstant: 150).isActive = true //Height
containerView.addSubview(nameTextField)
containerView.addSubview(nameSeperator)
containerView.addSubview(emailTextField)
containerView.addSubview(eMailSeperator)
containerView.addSubview(passwordTextField)
nameTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 12).isActive = true
nameTextField.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
nameTextField.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
nameTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor, multiplier: 1/3).isActive = true
nameSeperator.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
nameSeperator.topAnchor.constraint(equalTo: nameTextField.bottomAnchor).isActive = true
nameSeperator.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
nameSeperator.heightAnchor.constraint(equalToConstant: 1).isActive = true
emailTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 12).isActive = true
emailTextField.topAnchor.constraint(equalTo: nameTextField.bottomAnchor).isActive = true
emailTextField.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
emailTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor, multiplier: 1/3).isActive = true
eMailSeperator.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
eMailSeperator.topAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true
eMailSeperator.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
eMailSeperator.heightAnchor.constraint(equalToConstant: 1).isActive = true
passwordTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 12).isActive = true
passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true
passwordTextField.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
passwordTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor, multiplier: 1/3).isActive = true
}
}
这是错误:
2020-10-25 15:49:26.159731+0100 ChatY[27152:1748028] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003f9d360 h=--& v=--& UIView:0x7f9a6a4127f0.minY == 0 (active, names: '|':UIView:0x7f9a6a410250 )>",
"<NSLayoutConstraint:0x600003fa05f0 UIView:0x7f9a6a410250.height == 150 (active)>",
"<NSLayoutConstraint:0x600003fa0690 V:|-(0)-[UITextField:0x7f9a6a506e00] (active, names: '|':UIView:0x7f9a6a410250 )>",
"<NSLayoutConstraint:0x600003fa0730 UITextField:0x7f9a6a506e00.height == 0.333333*UIView:0x7f9a6a410250.height (active)>",
"<NSLayoutConstraint:0x600003fa07d0 V:[UITextField:0x7f9a6a506e00]-(0)-[UIView:0x7f9a6a4127f0] (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003fa07d0 V:[UITextField:0x7f9a6a506e00]-(0)-[UIView:0x7f9a6a4127f0] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-25 15:49:26.160930+0100 ChatY[27152:1748028] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003f9d4f0 h=--& v=--& UIView:0x7f9a6a4127f0.height == 0 (active)>",
"<NSLayoutConstraint:0x600003fa08c0 UIView:0x7f9a6a4127f0.height == 1 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003fa08c0 UIView:0x7f9a6a4127f0.height == 1 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-25 15:49:26.169639+0100 ChatY[27152:1748028] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003f9d950 h=--& v=--& UIView:0x7f9a6a414a30.minY == 0 (active, names: '|':UIView:0x7f9a6a410250 )>",
"<NSLayoutConstraint:0x600003fa05f0 UIView:0x7f9a6a410250.height == 150 (active)>",
"<NSLayoutConstraint:0x600003fa0690 V:|-(0)-[UITextField:0x7f9a6a506e00] (active, names: '|':UIView:0x7f9a6a410250 )>",
"<NSLayoutConstraint:0x600003fa0730 UITextField:0x7f9a6a506e00.height == 0.333333*UIView:0x7f9a6a410250.height (active)>",
"<NSLayoutConstraint:0x600003fa0960 V:[UITextField:0x7f9a6a506e00]-(0)-[UITextField:0x7f9a6a412960] (active)>",
"<NSLayoutConstraint:0x600003fa0820 UITextField:0x7f9a6a412960.height == 0.333333*UIView:0x7f9a6a410250.height (active)>",
"<NSLayoutConstraint:0x600003fa0a50 V:[UITextField:0x7f9a6a412960]-(0)-[UIView:0x7f9a6a414a30] (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003fa0a50 V:[UITextField:0x7f9a6a412960]-(0)-[UIView:0x7f9a6a414a30] (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-25 15:49:26.175688+0100 ChatY[27152:1748028] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003f9dc20 h=--& v=--& UIView:0x7f9a6a414a30.height == 0 (active)>",
"<NSLayoutConstraint:0x600003fa0af0 UIView:0x7f9a6a414a30.height == 1 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003fa0af0 UIView:0x7f9a6a414a30.height == 1 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-25 15:49:26.183282+0100 ChatY[27152:1748028] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003f9d2c0 h=--& v=--& UIView:0x7f9a6a4127f0.width == 0 (active)>",
"<NSLayoutConstraint:0x600003fa0550 UIView:0x7f9a6a410250.width == UIView:0x7f9a6a70ba30.width - 30 (active)>",
"<NSLayoutConstraint:0x600003fa0870 UIView:0x7f9a6a4127f0.width == UIView:0x7f9a6a410250.width (active)>",
"<NSLayoutConstraint:0x600003fa1220 'UIView-Encapsulated-Layout-Width' UIView:0x7f9a6a70ba30.width == 375 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003fa0870 UIView:0x7f9a6a4127f0.width == UIView:0x7f9a6a410250.width (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-25 15:49:26.184302+0100 ChatY[27152:1748028] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600003f9d630 h=--& v=--& UIView:0x7f9a6a414a30.width == 0 (active)>",
"<NSLayoutConstraint:0x600003fa0550 UIView:0x7f9a6a410250.width == UIView:0x7f9a6a70ba30.width - 30 (active)>",
"<NSLayoutConstraint:0x600003fa0aa0 UIView:0x7f9a6a414a30.width == UIView:0x7f9a6a410250.width (active)>",
"<NSLayoutConstraint:0x600003fa1220 'UIView-Encapsulated-Layout-Width' UIView:0x7f9a6a70ba30.width == 375 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003fa0aa0 UIView:0x7f9a6a414a30.width == UIView:0x7f9a6a410250.width (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
这是有局限性的,但是几个小时后,我仍然不知道如何解决它。
感谢您的帮助。 最好的