In attempt to improve the UI for smaller devices, when a UIScrollView contentSize.height is greater than it's bounds.height (I'm calling this an overflow) I want to tweak the UI.
My issue is: I'm finding the layout of the views is different after appearing on screen (in viewDidAppear(:)) than it is in the last viewDidLayoutSubviews() (could be called multiple times).
In order to determine if a scrollView is overflowing, we need to know both the scrollView.bounds and scrollView.contentSize. Which I thought would be determined in the last call of viewDidLayoutSubviews().
Below examples my scenario. It's running an interface built in xib with the scrollView set up there too. Note there are no hiding/showing of views to interfere with the layout values, simply runs through.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
printOverflow(type: #function)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
printOverflow(type: #function)
}
private func printOverflow(type: String) {
let isOverflowY = scrollView.contentSize.height > scrollView.bounds.height
debugPrint("[Overflow] \(type) \(isOverflowY ? "Overflow" : "Fits")")
}
// After running prints:
// "[Overflow] viewDidLayoutSubviews() Fits"
// "[Overflow] viewDidAppear(_:) Overflow"
My question is: why can I not get the final layout of the subviews in the last viewDidLayoutSubviews()?
I also tried adding a view.layoutIfNeeded() in viewWillAppear(:), indeed this fires another viewDidLayoutSubviews() as you'd expect but the bounds properties are still different to that in viewDidAppear(:) (despite actually now printing "Overflow")
From Apple's docs (italics are mine):
func viewDidLayoutSubviews()
When the bounds change for a view controller'€™s view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view'€™s subviews have been adjusted. Each subview is responsible for adjusting its own layout.
So, viewDidLayoutSubviews() can (and will) be called before subviews have been completely configured by auto-layout. That's also why we see viewDidLayoutSubviews() being called multiple times during loading / displaying.
If you subclass UIScrollView like this:
class MyScrollView: UIScrollView {
override func layoutSubviews() {
super.layoutSubviews()
printOverflow(type: #function)
}
private func printOverflow(type: String) {
let isOverflowY = contentSize.height > bounds.height
debugPrint("MyScrollView {Content Size: \(contentSize.height) Bounds Height: \(bounds.height)} [Overflow] \(type) \(isOverflowY ? "Overflow" : "Fits")")
}
}
You'll see that you get the correct heights.
What exactly are trying to do to "improve the UI for smaller devices"? There may be other (better?) options...
Edit
Here's a very simple example showing the sizing at each stage. The custom view changes its height anchor (if necessary) in its layoutSubviews() function:
import UIKit
class MySizingView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
if frame.height != 200.0 {
heightAnchor.constraint(equalToConstant: 200.0).isActive = true
}
print(#function, "self height:", frame.height)
}
}
class SizingTestViewController: UIViewController {
let myView: MySizingView = {
let v = MySizingView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .cyan
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
NSLayoutConstraint.activate([
// constrain width and center X & Y
// its height will be set by its own height constraint
myView.widthAnchor.constraint(equalToConstant: 300.0),
myView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
myView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(#function, "myView height:", myView.frame.height)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print(#function, "myView height:", myView.frame.height)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(#function, "myView height:", myView.frame.height)
}
}
Debug console output should be:
viewWillAppear(_:) myView height: 0.0
viewDidLayoutSubviews() myView height: 0.0
layoutSubviews() self height: 0.0
viewDidLayoutSubviews() myView height: 200.0
layoutSubviews() self height: 200.0
viewDidAppear(_:) myView height: 200.0
Related
How can I make cursor blink when the view is opened without touching anywhere. I am currently using becomeFirstResponder() My text field becomes really first responder but when the view is opened, blink does not blink. Why?
My code is here.
import UIKit
class ViewController: UIViewController {
let resultTextField: UITextField = {
var myField = UITextField()
myField.textColor = .blue
myField.font = .systemFont(ofSize: 36)
myField.textAlignment = .right
myField.borderStyle = .roundedRect
return myField
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(resultTextField)
resultTextField.becomeFirstResponder()
resultTextField.inputView = UIView()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let myWidth = self.view.frame.width
let myHeight = self.view.frame.height
resultTextField.frame = CGRect(x: (myWidth - (myWidth / 1.15))/2,
y: myHeight / 7.5,
width: myWidth / 1.15,
height: myHeight / 15)
}
}
Thanks
I agree with #Luca, try calling becomeFirstResponder in viewDidAppear or in your layoutSubviews function. viewDidLoad()only means that your UIViewController has been loaded to memory but is not displayed yet. Easy way to verify this is implementing a subclass from UITextField and override the caretRect which returns the cursor's frame. Set a breakpoint and test the code ...
class CustomTextField: UITextField {
override func caretRect(for position: UITextPosition) -> CGRect {
/// set a breakpoint and you will notice that it's not called with the viewDidLoad() of your UIViewController
return super.caretRect(for: position)
}
}
I was messing with layoutSubviews method in UIViewControllers. I assumed that when you override layoutSubviews on the view, it doesn't layout its subviews, but that wasn't the case, the view and its subviews were correctly laid out.
class Vieww: UIView {
override func didMoveToSuperview() {
super.didMoveToSuperview()
let centered = UIView()
addSubview(centered)
centered.translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .green
centered.backgroundColor = .red
centered.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1/2).isActive = true
centered.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1/2).isActive = true
centered.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
centered.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
override func layoutSubviews() {
print("layoutSubviews")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.layout(Vieww()).center().leading(20).trailing(20).height(400)
}
}
I expected that because I was not calling layoutSubviews, my views wouldn't be laid out, but I get layout like if I did override this method.
Read the docs: https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews
"the default implementation uses any constraints you have set to determine the size and position of any subviews. Subclasses can override this method as needed to perform more precise layout of their subviews."
My first question on SO so bear with me. I have created a UICollectionViewController which has a header and 1 cell. Inside the cell is a tableview, inside the table view there are multiple static cells. One of those has a horizontal UICollectionView with cells which have UITextViews.
Problem: When tapping on a UITextView the collection view scrolls/jumps
Problem Illustration
On the right you can see the y offset values. On first tap it changes to 267 -- the header hight. On a consecutive tap it goes down to 400 -- the very bottom. This occurs no matter what I tried to do.
Note: Throughout my app I'am using IQKeyboardManager
What have I tried:
Disabling IQKeyboardManager completely and
Taping on text view
Replacing it with a custom keyboard management methods based on old SO answers
Set collectionView.shouldIgnoreScrollingAdjustment = true for:
all scrollable views in VC
Individuals scrollable views
Note: this property originates from the IQKeyboardManager Library and as far as I understand it is supposed to disable scroll adjustment offset.
Tried disabling scroll completely in viewDidLoad() as well as all other places within this VC. I used:
collectionView.isScrollEnabled = false
collectionView.alwaysBounceVertical = false
Notably, I have tried disabling scroll in text viewDidBeginEditing as well as the custom keyboard management methods.
My Code:
The main UICollectionView and its one cell are created in the storyboard. Everything else is done programatically. Here is the flow layout function that dictates the size of the one and only cell:
extension CardBuilderCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = view.frame.size.height
let width = view.frame.size.width
return CGSize(width: width * cellWidthScale, height: height * cellHeigthScale)
}
}
Additionally, collectionView.contentInsetAdjustmentBehavior = .never
The TableView within the subclass of that one cell is created like so:
let tableView: UITableView = {
let table = UITableView()
table.estimatedRowHeight = 300
table.rowHeight = UITableView.automaticDimension
return table
}()
and:
override func awakeFromNib() {
super.awakeFromNib()
dataProvider = DataProvider(delegate: delegate)
addSubview(tableView)
tableView.fillSuperview() // Anchors to 4 corners of superview
registerCells()
tableView.delegate = dataProvider
tableView.dataSource = dataProvider
}
The cells inside the table view are all subclasses of class GeneralTableViewCell, which contains the following methods which determine the cells height:
var cellHeightScale: CGFloat = 0.2 {
didSet {
setContraints()
}
}
private func setContraints() {
let screen = UIScreen.main.bounds.height
let heightConstraint = heightAnchor.constraint(equalToConstant: screen*cellHeightScale)
heightConstraint.priority = UILayoutPriority(999)
heightConstraint.isActive = true
}
The height of the nested cells (with TextView) residing in the table view is determined using the same method as the one and only cell in the main View.
Lastly the header is created using a custom FlowLayout:
class StretchyHeaderLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let layoutAttributes = super.layoutAttributesForElements(in: rect)
layoutAttributes?.forEach({ (attribute) in
if attribute.representedElementKind == UICollectionView.elementKindSectionHeader && attribute.indexPath.section == 0 {
guard let collectionView = collectionView else { return }
attribute.zIndex = -1
let width = collectionView.frame.width
let contentOffsetY = collectionView.contentOffset.y
print(contentOffsetY)
if contentOffsetY > 0 { return }
let height = attribute.frame.height - contentOffsetY
attribute.frame = CGRect(x: 0, y: contentOffsetY, width: width, height: height)
}
})
return layoutAttributes
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
This is my first time designing a complex layout with mostly a programatic approach. Hence it is possible that I missed something obvious. However, despite browsing numerous old questions I was not able to find a solution. Any solutions or guidance is appreciated.
Edit:
As per request here are the custom keyboard methods:
In viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
Then:
var scrollOffset : CGFloat = 0
var distance : CGFloat = 0
var activeTextFeild: UITextView?
var safeArea: CGRect?
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
var safeArea = self.view.frame
safeArea.size.height += collectionView.contentOffset.y
safeArea.size.height -= keyboardSize.height + (UIScreen.main.bounds.height*0.04)
self.safeArea = safeArea
}
}
private func configureScrollView() {
if let activeField = activeTextFeild {
if safeArea!.contains(CGPoint(x: 0, y: activeField.frame.maxY)) {
print("No need to Scroll")
return
} else {
distance = activeField.frame.maxY - safeArea!.size.height
scrollOffset = collectionView.contentOffset.y
self.collectionView.setContentOffset(CGPoint(x: 0, y: scrollOffset + distance), animated: true)
}
}
// prevent scrolling while typing
collectionView.isScrollEnabled = false
collectionView.alwaysBounceVertical = false
}
#objc func keyboardWillHide(notification: NSNotification) {
if distance == 0 {
return
}
// return to origin scrollOffset
self.collectionView.setContentOffset(CGPoint(x: 0, y: scrollOffset), animated: true)
scrollOffset = 0
distance = 0
collectionView.isScrollEnabled = true
}
Finaly:
//MARK: - TextViewDelegate
extension CardBuilderCollectionViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
self.activeTextFeild = textView
configureScrollView()
}
}
The problem that I can see is you calling configureScrollView() when your textView is focused in textViewDidBeginEditing .
distance = activeField.frame.maxY - safeArea!.size.height
scrollOffset = collectionView.contentOffset.y
self.collectionView.setContentOffset(CGPoint(x: 0, y: scrollOffset + distance), animated: true)
You're calling collectionView.setContentOffset --> so that's why your collection view jumping.
Please check your distance calculated correctly or not. Also, your safeArea was modified when keyboardWillShow.
Try to disable setCOntentOffset?
I'm doing all my UI programatically, to avoid a massive view controller I have a class of type UIView where I'm declaring all my UI elements.
I'm declaring my scrollView like this:
class RegisterUIView: UIView {
lazy var scrollView: UIScrollView = {
let scroll: UIScrollView = UIScrollView()
scroll.contentSize = CGSize(width: self.frame.size.width, height: self.frame.size.height)
scroll.translatesAutoresizingMaskIntoConstraints = false
return scroll
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
])
}
}
After the declaration. I create an instance of RegisterUIView in my ViewController.
And in the viewWillAppear and viewWillDisappear I used the variable hidesBarsOnSwipe, to hide the navigation bar.
When I scroll down the bar hides, but when I scroll up the bar is not unhiding.
I read in other question here that I need to set the top constraint to the superview.
How can is set the constraints to the superview?, when I try to set it the app crashes, and is obviously because there is no superview.
class RegisterViewController: UIViewController {
private let registerView: RegisterUIView = {
let view: RegisterUIView = RegisterUIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.hidesBarsOnSwipe = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.hidesBarsOnSwipe = false
}
override func viewDidLoad() {
super.viewDidLoad()
setupLayout()
}
func setupLayout() {
view.addSubview(registerView)
NSLayoutConstraint.activate([
registerView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
registerView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
registerView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
registerView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor)
])
}
Add the scroll view to the subview before adding constraints. Your app crashes because you're adding constraints to an object, not in the subview.
view.addSubview(scrollView)
add that line of code to the top of your setupLayout() function before your start adding constraints.
import UIKit
class ViewController: UIViewController {
// MARK: -property
// lazy var testBtn: UIButton! = {
// var btn: UIButton = UIButton()
// btn.backgroundColor = UIColor.red
// print("testBtn lazy")
// return btn
// }()
// MARK: -life cycle
override func viewDidLoad() {
super.viewDidLoad()
print("View has loaded")
// set the superView backgroudColor
// self.view.backgroundColor = UIColor.blue
// add testBtn to the superView
// self.view.addSubview(self.testBtn)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("View will appear")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("View has appeared")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("View will disappear")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("View has desappeared")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print("SubViews will layout")
// layout subViews
// 'CGRectMake' is unavailable in Swift
// self.testBtn.frame = CGRectMake(100, 100, 100, 100)
// self.testBtn.frame = CGRect(x: 100, y: 100, width: 100, height: 100) // CGFloat, Double, Int
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("SubViews has layouted")
// let testBtn_Width = self.testBtn.frame.width
// print("testBtn's width is \(testBtn_Width)")
}
}
The result:
View has loaded
View will appear
SubViews will layout
SubViews has layouted
SubViews will layout
SubViews has layouted
View has appeared
As you see, I have created a new project and type some simple code.
I didn't change the size of the viewController's view.
Why are "SubViews has layouted" and "SubViews will layout" console two times?
Why are viewDidLayoutSubviews and viewWillLayoutSubviews called two times?
Because whenever setNeedsLayout or setNeedsDisplayInRect is being called internally, LayoutSubviews is also being called (once per run loop) on any given view. This applies for example if the view has been added, scrolled, resized, reused etc.