
A Scroll View is a container view that allows users to scroll content that is larger than the screen size (vertically or horizontally).
A Scroll View is a container view that allows users to scroll content that is larger than the screen size (vertically or horizontally).
It refers to adjusting the UI when the keyboard appears or disappears, so that input fields are not hidden.
π When combined: Scroll View + Keyboard handling = smooth form-based apps (login, registration, chat apps)
View Controller
β
Scroll View
β
Content View
β
UI Elements (TextFields, Buttons)
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
@IBOutlet weak var scrollView: UIScrollView!
@objc func keyboardWillShow(notification: Notification) {
if let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect {
let keyboardHeight = keyboardFrame.height
scrollView.contentInset.bottom = keyboardHeight
scrollView.verticalScrollIndicatorInsets.bottom = keyboardHeight
}
}
@objc func keyboardWillHide(notification: Notification) {
scrollView.contentInset.bottom = 0
scrollView.verticalScrollIndicatorInsets.bottom = 0
}
deinit {
NotificationCenter.default.removeObserver(self)
}
Draw:
Keyboard Appears
β
Scroll View Insets Increase
β
Content Moves Up
β
TextField Remains Visible
Problem:
Solution:
scrollView.contentInset.bottom = keyboardHeight
Scroll View = allows scrolling of large content
Used in forms and login screens
Keyboard handling prevents UI hiding
Steps:
Improves usability and user experience
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.