Monday, February 26, 2024
HomeiOS Developmentios - UITextField Jumps Above UIImageView When Keyboard Seems

ios – UITextField Jumps Above UIImageView When Keyboard Seems


The issue includes the format habits of a UITextField which inside an UIImageView. And that UIImageView is inside a UIScrollView. The aim is to have the textual content subject positioned on high of the picture view, and when the keyboard seems, the textual content subject ought to stay in place with out leaping over the picture view.

Nevertheless, when the keyboard is displayed, as an alternative of staying in its unique place on high of the picture view, the textual content subject strikes upward, passing over the picture view. Now this difficulty goes away in case you dismiss the keyboard. I’ve tried transferring the entire scrollview once we know the keyboard is being proven however that did not appear to work, or I wasn’t in a position to implement it accurately. Any Assist could be nice! thx!

import UIKit
import NotificationCenter
import Basis

class ZoomImage: UIViewController {
    var x: CGFloat = 100
    var y: CGFloat = -400
    var keyboardHeight: CGFloat = 0.0
    
    personal let textField: UITextField = {
        let ut = UITextField()
        ut.textColor = .white
        ut.placeholder = "Testing"
        ut.backgroundColor = .none
        ut.body = CGRect(x: 20, y: 20, width: 200, top: 30)
        return ut
    }()
    
    // create scrollView
    personal let scrollView: UIScrollView = {
        let sv = UIScrollView()
        sv.backgroundColor = .blue
        return sv
    }()
    
    // create imageView
    personal var imageView: UIImageView = {
        let v = UIImageView()
        v.isUserInteractionEnabled = true
        v.backgroundColor = .purple
        return v
    }()
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        registerObservers()
        setup()
    }
    
    func registerObservers() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)),
                                               title: UIResponder.keyboardWillShowNotification,
                                               object: nil)
    }
    
    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            keyboardHeight = keyboardSize.measurement.top
        }
    }
    
    deinit {
        // Unsubscribe from keyboard notifications
        NotificationCenter.default.removeObserver(self)
    }
    
    func setup() {
        // Permits us to have this view be the delegate of the textfield
        self.textField.delegate = self
        // Permits us to have this view be the delegate of the scrollview
        scrollView.delegate = self
        // add Scrollview to view
        self.view.addSubview(scrollView)
        
        // Defining imageView
        let picture = UIImage(named: "take a look at.png")!
        imageView.picture = picture
        imageView.contentMode = .scaleAspectFit
        
        // add UIImageView to scroll view
        scrollView.addSubview(imageView)
        imageView.addSubview(textField)
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        textField.translatesAutoresizingMaskIntoConstraints = false
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        
        // respect safe-area
        let safeG = view.safeAreaLayoutGuide
        
        // Zoom vary
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 10.0
        
        // constraints
        NSLayoutConstraint.activate([
            scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: safeG.leadingAnchor, constant: 0),
            scrollView.frameLayoutGuide.trailingAnchor.constraint(equalTo: safeG.trailingAnchor, constant: 0),
            scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: safeG.bottomAnchor, constant: 0),
            scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: safeG.topAnchor, constant: 0),

            imageView.leadingAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.leadingAnchor, constant: 0),
            imageView.trailingAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.trailingAnchor, constant: 0),
            imageView.bottomAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.bottomAnchor, constant: 0),
            imageView.topAnchor.constraint(equalTo: self.scrollView.contentLayoutGuide.topAnchor, constant: 0),

            imageView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
            imageView.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),

            textField.leadingAnchor.constraint(equalTo: imageView.leadingAnchor, constant: self.x),
            textField.bottomAnchor.constraint(equalTo: imageView.bottomAnchor, constant: self.y),
        ])
        
    }
}


// MARK: - Extensions
extension ZoomImage:  UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

extension ZoomImage: UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
            print("MOVE VIEW Down")
            if self.scrollView.body.origin.y == 0{
                self.scrollView.body.origin.y -= keyboardHeight
            }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("TextField ought to return methodology referred to as")
        textField.resignFirstResponder();
        return true;
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments