Prevent scrollview from autoresizing - ios

Im creating a basic messaging app with the message "bubbles" inside a scroll view. When the user first opens the screen the keyboard is not showing (besides an input box at the bottom), when they press the input box and start typing the keyboard with pop up (using NSNotificationCenter) a function will be called to resize the scrollview so that is no longer behind the keyboard.
That part all works well but when the user sends their message and a new bubble is added to the scrollview. The scrollview will automatically revert back to the size of the screen thus being behind the keyboard.
The scrollview is originally created in storyboard to take up the whole dimensions of the screen. So I suspect the cause of the resizing is due to some kind of message calling autolayout to update the scrollview when a subview is being added to it.
Im looking for a way to prevent the scroll view from reverting back to this autolayout when a subview is added to it, Thank you!

When the keyboard appears, adjust the UIScrollView's size by modifying its constraint's constants.

Related

How to control UIScrollView automatically scrolling on FirstResponder / Keyboard changes?

TL;DR
Is it a new feature in Xcode 14 and iOS 16 that a UIScrollView is now scrolled automatically when a view inside the ScrollView becomes firstReponder and the keyboard appears? While working on other iOS projects in earlier versions of Xcode and iOS I never came across this behaviour. Can this be controlled or deactivated?
Details:
While working with Xcode 14.0.1 on a new iOS project I noticed that an UIScrollView is now automatically scrolled when the content view becomes first responder.
Have a look at this example. I have created a new project, added a ScrollView to the ViewController and placed 12 subviews inside to fill it with some content. Subview no. 12 additionally holds a UITextField. The firstResponder status of this text field can be toggled using the top button.
When tapping on the button the TextField becomes firstResponder and the ScrollView is automatically scrolled up. However, not enough to make bring the TextField above the keyboard and make visible. Just enough to bring it above the bottom screen edge.
This does not make a lot of sense to me.
Is it possible to control this automatic scrolling or to deactivate it?
EDIT:
I have created CustomScrollView as UIScrollView subclass and did override scrollRectToVisible. When calling super.scrollRectToVisible everything works as before, but I could set a break point to see from where scrollRectToVisible is called.
The callstack shows the method scrollTextFieldToVisibleIfNecessary inside UITextField which uses delegateShouldScrollToVisibleWhenBecomingFirstResponder
However, UITextFileDelegate does not have such a method / value and scrollTextFieldToVisibleIfNecessary is also documented nowhere.
So, the question remains: How to disable this behaviour?

XCUITests: Can't tap element on scrollview while views are stack on each other

Sample Project
This is a sample project that showing the issue. It's storyboard based, but method of building interface doesn't matter. It's UIViewController with UIScrollView for entire screen and 128 pts height view that is on top of this UIScrollView.
Inside scroll view there is an UIView that has 2000 pts height and UIButton in the center.
Initial State
After light scroll
At the bottom of UIScrollView
Link here: https://github.com/JakubMazur/UITestsDemo
Problem
I'm trying to tap this green button with XCUITest using app.buttons["Tap Me!"].tap()
XCUITest get identifiers from elements on screen for entire scroll view that works fine.
According to this reply on a thread on Apple Developer Forum written by Apple Framework Engineer I shouldn't scroll manually to get to the button and yes, this is partially true.
What is happening when code from (1) is executed is that button is scrolled just enough to be visible on screen but it's still not hittable, because other (purple view) is on top of UIScrollView
What is working
If I run a test written like this:
func testThatDoWorkButItsSlow() {
app.scrollViews.firstMatch.swipeUp()
app.buttons[buttonLabel].tap()
}
that is scrolling up and then looks for a button this will work, but it's slow and so inaccurate that is hardly usable.
What I cannot do
Disabling userInteractions on purple view. In real example I still need touches for this (purple) view.
Questions
Is there a way to use precise scrolling in XCTest for this case?
Or is there a way to set contentOffset scrollview to other value that will make this button more centered on a screen compared to action of tap()?
Or there is a way to fast scroll to the bottom (without animations) and maybe moving only up for each element?
My recommendation here would be to use the XCUICoordinate.press(forDuration:thenDragTo:) method to scroll.
https://developer.apple.com/documentation/xctest/xcuicoordinate/1615003-press
You can create a XCUICoordinate for the yellow view, then drag it slightly upwards to expose the button and make it hittable.
In most cases, the automatic scroll should work, but it seems like in this case a manual scroll/drag is necessary.
The UI Testing should replicate human interactions. You cannot expect from a human being to scroll "153px", you can just expect to "scroll until".
You can try something like :
while (!app.buttons["Tap Me!"].isHittable) {
app.swipeUp()
}
NB: You may also want to add a condition to leave the while loop if you can't find the button after a reasonable amount of attempts

The event can be triggered after change the frame of superview

1.Show a popup when tap a button(KLCPopup)
2.Change the height of contentView(KLCpopup) when tap the 'change frame' button,and change the height successfully
3. Tap 'change frame' button again, I want change the height again, but it does not work.
the source is in https://github.com/leogeng/LabProject.git
Who can fix it?
I can fix it but it is best you do it. Let this answer be a bit more about how to debug your application.
After a very short analysis I put a breakpoint in your button action method and found out it is only called once. So it does not resize the second time because the button is not working at all.
What the candidates for that are is the button is being tempered with in ways such as interactions disabled, it is over-layed with another view which prevents the touch events, or its superviews are not correctly sized to detect touches.
After using the view debugger (there is an icon you may use in runtime which displays the view hierarchy) I can see the black button on the blue background which is on some transparent background whose size is too small and the blue view is out of bounds.
It seems you need to resize the superview of the blue view as well.

What is the best way to animate the size of sub view in a UIScrollView?

I've got a scrollview that allows the user to scroll between different pages and then tap on one to have it expand so that they can read the page in full, a little like how one changes tabs in Safari on the iPhone. Changing the frame size of each sub view is a bit of a pain when rotating as the scroll position is getting lost as the content size of the sub view has to change too. I was wondering if there was a more effective way of
resizing the views for entering 'viewing' mode or rotating the device.
The solution to your first problem is when you want to expand the view, pull it out of the scrollView then add it to self.view.subviews with an appropriate frame, then animate the frame to fill the screen. When done with it do the reverse, shrink it, then when its back to the appropriate size stick it back in the scrollView.
If for some reason this does not work - the scrollview is showing other stuff that gets moved when you remove the view, then instead of just removing your view from it, create a simple UIView of the same size as the view you will expand, and essentially replace the view you pull out with the "placeholder" view.

UIScrollView unwanted scrolling after addSubview or changing frame

I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.
But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.
Attempts:
pagingEnabled is always NO.
Setting scrollEnabled to NO during subview manipulations doesn't help.
Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
One single giant subview with all my subviews in it doesn't help.
My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...
Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?
Xcode 4.3, iOS SDK for 5.1.
I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html
It involves subclassing the UIScrollView and entering no code in the following method.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
}
Like the guy says on the link I've found it works and no problems so far. Hope it works for you.
I had this problem because I set the content size of the scroll view prior to adding the subview.
As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.

Resources