Это переменные, которые я использую в этой части кода, где yCo
- это координата y, которая мне нужна для ландшафтного режима, а yCoP
- это координата y, которая мне нужна для портретного режима. У меня BottomView
внизу экрана который содержит messageTF
текстовое представление, и оба должны быть изменены при увеличении / уменьшении содержания текстового представления.
var yCo = CGFloat()
var yCoP = CGFloat()
var userInfo2 = [AnyHashable : Any]()
@IBOutlet weak var BottomView: UIView!
@IBOutlet weak var messageTF: UITextView!
Это те суперклассы, которые я использую
override func viewDidLoad()
{
super.viewDidLoad()
BottomView.translatesAutoresizingMaskIntoConstraints = false
messageTF.delegate = self
messageTF.text = "Type Your Message"
messageTF.textColor = UIColor.lightGray
self.hideKeyboardWhenTapped()
}
override func viewDidAppear(_ animated: Bool)
{
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHideNotification(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool)
{
NotificationCenter.default.removeObserver(self)
}
Это функции, которые я использую для перемещения вида при появлении и закрытии клавиатуры.
@objc func keyboardHideNotification(notification : NSNotification)
{
if let userInfo = notification.userInfo {
userInfo2 = userInfo
let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if UIDevice.current.orientation.isLandscape {
print("Landscape")
self.BottomView.frame.origin.y = endFrameY - self.BottomView.frame.height
} else {
print("Portrait")
self.BottomView.frame.origin.y = endFrameY - self.BottomView.frame.height - 16
}
UIView.animate(withDuration: duration,
delay: TimeInterval(0),
options: animationCurve,
animations: { self.view.layoutIfNeeded() },
completion: nil)
}
}
func hideKeyboardWhenTapped() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKey))
tap.cancelsTouchesInView = false
msgCollectionV.addGestureRecognizer(tap)
let tap2: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKey))
tap2.cancelsTouchesInView = false
welcomeView.addGestureRecognizer(tap2)
}
@objc func dismissKey() {
view.endEditing(true)
}
Это функции делегата TextView, которые я использую для наблюдения изменения в текстовом представлении
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray
{
textView.text = nil
textView.textColor = UIColor.black
if textView.text == nil
{
uploadLbl.isHidden = true
}
}
if textView.textColor == UIColor.black && textView.text != ""
{
uploadLbl.isHidden = false
}
}
func textViewDidChange(_ textView: UITextView) {
self.adjustTextViewHeight()
if textView.text == "" && photoArray.count == 0 && audioRecorded == false
{
uploadLbl.isHidden = true
}
else
{
uploadLbl.isHidden = false
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == "" && photoArray.count == 0 && audioRecorded == false
{
textView.text = "Type Your Message"
textView.textColor = UIColor.lightGray
uploadLbl.isHidden = true
}
else
{
uploadLbl.isHidden = false
}
}
Это функции, которые я использую для изменения размера Text View
и Bottom View
.
func frameYCoordinates()
{
let fixedWidth = messageTF.frame.size.width
let newSize = messageTF.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
let endFrame = (userInfo2[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
yCo = endFrameY - BottomView.frame.height
yCoP = endFrameY - BottomView.frame.height - 16
print(yCoP)
if UIDevice.current.orientation.isLandscape
{
BottomView.frame = CGRect(x: BottomView.frame.origin.x, y: yCo, width: BottomView.frame.size.width, height: newSize.height + 26)
}
else
{
BottomView.frame = CGRect(x: BottomView.frame.origin.x, y: yCoP, width: BottomView.frame.size.width, height: newSize.height + 26)
}
}
func adjustTextViewHeight() {
let endFrame = (userInfo2[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
let endFrameY = endFrame?.origin.y ?? 0
yCo = endFrameY - BottomView.frame.height
yCoP = endFrameY - BottomView.frame.height - 16
let fixedWidth = messageTF.frame.size.width
let newSize = messageTF.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
if messageTF.numberOfLines() <= 4
{
messageTF.frame = CGRect(x: messageTF.frame.origin.x, y: messageTF.frame.origin.y, width: messageTF.frame.size.width, height: newSize.height)
BottomView.frame = CGRect(x: BottomView.frame.origin.x, y: yCoP, width: BottomView.frame.size.width, height: newSize.height + 26)
print(yCoP)
frameYCoordinates()
}
}
Я использую это для вычисления количества строк в текстовом представлении
extension UITextView{
func numberOfLines() -> Int{
if let fontUnwrapped = self.font{
return Int(self.contentSize.height / fontUnwrapped.lineHeight)
}
return 0
}
}
Теперь проблема, с которой я сталкиваюсь, заключается в том, что приведенный выше код работает отлично и изменяет размеры обоих textView
и BottomView
in iPhone 6 (iOS 12.4) но в iPhone 7 (iOS 13.3), как только я наберу один алфавит, BottomView
переместится в нижнюю часть superView, и изменение размеров представлений не произойдет.
Может кто-нибудь сказать, почему это не работает в iOS 13, но работает на iOS 12?