У меня есть UIViewController
с 3 UITextFields
и UITextView
.Обязательный контент, который мне нужен, это либо UITextFields
, называемый postTitleTextField
, ИЛИ единственный UITextView
, в котором есть контент до включения postButton типа UIBarButtonItem
.Я делаю что-то не так, и это не работает так, как я ожидаю.
import UIKit
class PostTextVC: UIViewController {
@IBOutlet weak var postButton: UIBarButtonItem!
@IBOutlet weak var postTitleTextField: UITextField!
@IBOutlet weak var postContentTextView: UITextView!
@IBOutlet weak var postTagsTextField: UITextField!
@IBOutlet weak var postSourceTextField: UITextField!
var textViewPlaceholderLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.barTintColor = PostMenuColors.yellow2
navigationController?.navigationBar.tintColor = .white
setupTextFields()
setupContentTextView()
textFielsdDelegate()
}
@IBAction func cancelButtonPressed(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
@IBAction func optionsButtonPressed(_ sender: UIBarButtonItem) {
}
@IBAction func postButtonPressed(_ sender: UIBarButtonItem) {
print("Post pressed")
}
}
extension PostTextVC: UITextFieldDelegate
{
func setupTextFields()
{
setupTextFieldUI(textField: postTitleTextField)
setupTextFieldUI(textField: postSourceTextField)
setupTextFieldUI(textField: postTagsTextField)
postButton.isEnabled = false
// handleTextFields()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
// Note: Listens for changes for ALL text frields
func handleTextFields()
{
postTitleTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
}
func textFielsdDelegate()
{
postTitleTextField.delegate = self
postTagsTextField.delegate = self
postSourceTextField.delegate = self
}
// Note: Checks for value in all text fields and/or disable/enable the sign in button
@objc func textFieldDidChange()
{
guard let postTitle = postTitleTextField.text, !postTitle.isEmpty
else
{
self.postButton.tintColor = UIColor.lightGray
self.postButton.isEnabled = false
return
}
guard let postContent = postContentTextView.text, !postContent.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty
else
{
self.postButton.tintColor = UIColor.lightGray
self.postButton.isEnabled = false
return
}
postButton.isEnabled = true
postButton.tintColor = Colors.mainBlueColor
}
func setupTextFieldUI(textField: UITextField)
{
textField.tintColor = .darkGray
textField.textColor = .darkGray
textField.borderStyle = .none
let bottomLine = CALayer()
bottomLine.frame = CGRect(x: 0, y: 29, width: textField.bounds.width, height: 1)
bottomLine.backgroundColor = UIColor.gray.cgColor
textField.layer.addSublayer(bottomLine)
textField.backgroundColor = .clear
textField.attributedPlaceholder = NSAttributedString(string : textField.placeholder!,
attributes: [NSAttributedStringKey.foregroundColor: Colors.authTextFieldPlaceholderColor])
}
}
// MARK: Text View
extension PostTextVC : UITextViewDelegate {
func setupContentTextView()
{
postContentTextView.delegate = self
textViewPlaceholderLabel = UILabel()
textViewPlaceholderLabel.text = "Enter your thoughts"
textViewPlaceholderLabel.font = UIFont.init(name: Fonts.OpenSans_Regular, size: (postContentTextView.font?.pointSize)!)
textViewPlaceholderLabel.sizeToFit()
postContentTextView.addSubview(textViewPlaceholderLabel)
textViewPlaceholderLabel.frame.origin = CGPoint(x: 0, y: (postContentTextView.font?.pointSize)! / 2)
textViewPlaceholderLabel.textColor = UIColor.lightGray
textViewPlaceholderLabel.isHidden = !postContentTextView.text.isEmpty
}
func textViewDidChange(_ textView: UITextView) {
textViewPlaceholderLabel.isHidden = !postContentTextView.text.isEmpty
}
}