Programatically changing the ContentInset of a UIScrollView doesn't actually change anything - swift

I have a UIScrollView created in Interface builder, now in that UIScrollView there's a UITextField. When the keyboard opens, I'd like to change the ContentInset of the UIScrollView to fit the keyboard.
I've done this previously through a package called RXKeyboard, which reactively gives me the height of the keyboard:
RxKeyboard.instance.visibleHeight
.drive(onNext: { height in
self.scrollView.contentInset.bottom = height
print("offset", self.scrollView.contentInset)
}).disposed(by: disposeBag)
Now the ContentInset of the UIScrollView does change (shown through the print), however, nothing actually changes, and the bottom part of my UIScrollViewis hidden by the keyboard...
Am I missing something here? I tried moving this into viewDidLayoutSubviews, I tried layoutSubviews() after setting the inset, but I can't get this to work.
Does anyone know a solution to this?
Thanks!

That's right you've set your scrollView's insets to be keyboard's height but as the keyboard comes up you want to make your scrollView Content to come up to keyboard's height.
You have to scroll your scrollView to keyboard height by adjusting it's contentOffset property.
RxKeyboard.instance.willShowVisibleHeight
.drive(onNext: { keyboardVisibleHeight in
self.scrollView.contentOffset.y += keyboardVisibleHeight
})
.disposed(by: disposebag)
As the documentation says,
The point at which the origin of the content view is offset from the origin of the scroll view.
By setting contentOffset.y to keyboard's height, you are telling scrollView to move/scroll to that height.

Related

Controls at the bottom of UIScrollView not working

The UIButton in the scrollView is visible, but not accessible. I am using Constraints.
My UI structure is this:
- UIView
- scrollView: UIScrollView
- contentView: UIView
- UIButton
- UIButton
- UIButton
- ....
- UIButton
I've already tried to set the contentSize of the scrollView. And the height of the contentView. Next to that I've tried to uncheck the checkbox Adjust Scroll View Insets in the storyboard of that ViewController without any luck. I've also set the priority of the Align Center Y to 250, and the priority bottom space to 250 of the contentView.
func updateScrollViewSize() {
var contentRect = CGRect.zero
for view in contentView.subviews {
contentRect = contentRect.union(view.frame)
}
contentRect.size = CGSize(width: scrollView.contentSize.width, height: contentRect.height + 50)
scrollView.contentSize = contentRect.size
contentView.frame.size = contentRect.size
contentView.layoutIfNeeded()
}
The button I try to reach has a Y value of: 1030.0
The height of the contentView is: 871.0
Step-by-step:
Add a scroll view to your view, background color red, constrain it 20-pts on each side:
Add a UIView as your "content view" to the scroll view (green background to make it easy to see), constrain it 0-pts on each side, constrain equal width and equal height to the scroll view. Important: change the Priority of the Height constraint to 250!
Add a UILabel to the contentView, constrain it 30-pts from the top, centered horizontally:
Add another label to the contentView, constrain it 300-pts from the first label, centered horizontally:
Add a UIButton to the contentView, constrain it 30-pts from the bottom, centered horizontally:
Now add a vertical constraint from the bottom of the second label to the top of the button, and set it to 400-pts:
As you see, this pushes the button off-screen past the bottom of the scroll view.
Run the app. You will be able to scroll down to see the button, and you'll be able to tap it.
Absolutely no code needed!
If you use AutoLayout, you don't needed to install frames manually.
You can try install constraints properly and content size will be right in this case and you won't have to install content size manually.
If you achieve this, I guess everything will work correctly.
You can follow this or this guide

unable to scroll the textview in Swift

I have a TextView as shown below
I am not able to scroll the text view , I have added UITextViewDelegate in UIViewController class as well as set isUserInteractionEnabled property in textViewDidBeginEditing
func textViewDidBeginEditing(_ textView: UITextView) {
textView.backgroundColor = UIColor.lightGray
textView.isEditable = false
textView.isUserInteractionEnabled = true
textView.isScrollEnabled = true
}
What did I need to do?
Also, the scrolling is enabled in attribute inspector
This issue has occurred because the actual UITextView's size was more than screen size as shown below
The real answer is that the UITextView needs its content frame height inferior to its content size height to be able to scroll.
In your case, the content frame height is equal to the content size, so it doesn't scroll.
You just have to set the leading left, trailing, top space and bottom space to the View, but first make sure the Text View is smaller than the actual View (parent).
It starts to get complicated when you have a UITextView on a UIView which is either a view on a UIScrollView or directly on a UIScrollview.
The best thing to do, is to give each of them their own actions when scrolling.
Firstly, make sure you have all of your delegates for EVERYTHING set.
Make sure your UITextViews are not user restricted for what they don't need to be.
What I have to do in one of my published apps is this👇
func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
{
if scrollView == textView1
{
// Do something if you actually want to, or just let textView1 scroll as intended
}
else if scrollView == textView2
{
// Do something if you actually want to, or just let textView2 scroll as intended
}
else if scrollView == zoomingScroll
{
// Do something if you need to or leave it
}
else
{
// Do something with all the other scrollable views if you need to
}
}

Scroll in UIScrollView with tvOS

I stuck with scrolling content inside UIScrollView in my tvOS app.
I have scrollView with height = 400 and width = 400. Inside this scrollview I have non-scrollable UITextView with height = 800 and width = 400. So I want to scroll this text.
But my scroll view is not scrolled, I don't see any scrolling indicators and also my scrollView have isFocused value = false. How can I solve this problem?
p.s. update I created separate ViewController (image below).
It have black ScrollView and white view with big height with label in the middle of it. ScrollView has fixed width and height and there is no scrolling for some reason! I even didn't connect any separate class - just created it from Interface builder.
UIView isn't focusable by default (and as such in tvOS it can't be scrolled) . You have to subclass it to override canBecomeFocused:
class myUIView: UIView {
override var canBecomeFocused: Bool {
return true
}
}
Then in your example use the class myUIView instead of UIView for your long white view. Setting the right constraints you wouldn't need to design the views out of the controller view boundaries in IB either. Check this answer with the linked gist to see how to build the constraints.
I think you can use the
func gestureRecognizer (_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
method to judge the view and scroll it
According to the view you touch to do something to intercept
There is some way to fix your problem.
Please check your scrollview in storyboard, is your scrolling or vertical indicator is checked?
U can make your textview scrollable if u just want to scroll the textview content
There is problem in your constraint, you need to cleary add the height, vertical space top, vertical space bottom from your textview to your scrollview.
Hope these advice can help you
So you have to set the scrollview's content size larger than the scrollview bounds, so you should set contentsize to (400, 800), probaly in you xib

Scrolling ScrollView even when ContentView has the same size

I have a ScrollView defined with autolayout : it has a contentView, in which there's some elements. The scrollView has constraints with other views to set it's size, and 4 constraints with the contentView (up, down, left, right, each with constant = 0). The contentView has a defined size, the same size as the scrollView.
I want my scrollView to scroll and bounce (I overrided the scrollViewDidScroll method), but the problem is the scrollView is not scrolling if the contentView has the same size as it (which is logic, since there's no content to scroll, but I want it to bounce).
How can I make my scrollView scrolling ?
Try setting to your scrollView:
scrollView.alwaysBounceHorizontal = true
scrollView.alwaysBounceVertical = true
or in Interface Builder, make sure you've checked options:

Right part of UIScrollView doesn't work in Landscape mode

Using the Interface Builder, I created a view with a UIScrollView in it.
I programmaticly add the buttons to the empty UIScrollView.
When the orientation changes, I use
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
to call a method that resets the buttons on screen.
After that, the uiScrollView-content gets a new size using setContentSize.
No matter the width of the ContentSize, I can only interact (scroll/or tap a button) on the first 320px of the screen - which is the screen width in portrait mode.
When I set the contentSize-width to 2000, I can scroll to the left, but only with my fingers on the first 320 px instead of the full 480 (using a 3.5 inch iPhone).
What am I missing?
You need to resize the scrollview's frame, not just the content size (in fact, often you don't need to resize the content size, as the content may not change following an orientation change, but just the frame).
I had the same problem with a Popup (UIView) that was called from a (UIViewController).
In the UIViewController the popup was created as followed
func createPopup() {
let myPopup = MyPopup(frame: UIScreen.mainScreen().bounds)
self.view.addSubview(myPopup)
}
In the Popup (UIView) I needed to override layoutSubviews as follows:
override func layoutSubviews() {
super.layoutSubviews()
self.setNeedsDisplay()
let mainScreenBounds = UIScreen.mainScreen().bounds
view.frame = mainScreenBounds
super.frame = mainScreenBounds <-- Need to update the parent
** perform additional rotation/orientation code here **
}