How to scroll to button's position in scrollView - swift

I have scrollView and button at about at middle at scrollView. I want to scroll to this button, using button y-position, how can i do it?
I can scroll to the top or bottom of the scrollView, but I can't don't know how to scroll to the exact position of some element
Image example

Use UIScrollView
open func scrollRectToVisible(_ rect: CGRect, animated: Bool) method.
This method allows you to scroll to a particular frame.
scrollView.scrollRectToVisible(/*Button frame*/, animated: true)
Note:
Suppose your button is in multiple subviews then you first need to find the button frame from the scrollView and then scroll.
let buttonFrame = yourButton.superview?.convert(yourButton.frame, from: scrollView)
scrollView.scrollRectToVisible(buttonFrame ?? .zero, animated: true)

Related

Scroll to top in scrollview when click on any button in swift

In a ScrollView I am showing list of views, and at the bottom of that ScrollView there is a CollectionView. When I click any item in the CollectionView, ScrollView needs to move to the top. However, it's not scrolling.
I Have been using below code in action:
let topOffset = CGPoint(x: 0, y: 0)
scrollView.setContentOffset(topOffset, animated: true)
self.viewDidLoad()
self.viewWillAppear(true)
Thanking you in advance

How can I always show the UIScrollBar indicator?

When a UIViewController with a UIScrollView is visible on screen, I want to show the UIScrollView scrollbar indicator.
Is there a way to show the indicator?
You can show the scrollbar momentarily with .flashScrollIndicators when the view controller has been added to the view hierarchy and is potentially visible to the user.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.flashScrollIndicators()
}

How I can show/hide view inside another view with height changing of root swift?

I have two views one inside another, it looks like picture below:
when I press on orange arrow I would like to show/hide view above grey line and grey line too. I did it by such way:
#objc func showHide(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.jobDataView.isHidden = !self.jobDataView.isHidden
self.view.layoutIfNeeded()
})
tappedImage.image = self.jobDataView.isHidden ? UIImage(systemName: "arrow.down"):UIImage(systemName: "arrow.up")
}
and my view above gray line can be hidden and shown. But root view doesn't change its' height and it has similar sizes before and after btn click. I tried to add constraint of height, but I it didn't solve my problem. Maybe someone knows how to solve my problem?
You need to use UIStackView either through Storyboard or from code. when you hide subview inside stackview it will automatically change stackview height. what you need to do is to make stackView distribution = fill and use vertical stack...
Hide view on button tap and when you need to show it .. add it in stack at. 0th index ..

Scroll to a UILabel location within a UIScrollView

I was wondering if it is possible to scroll to the location of a UILabel that is located within a ScrollView. This is what I have tried so far but I can't get it to work. I'm also shifting the view when the user begins to edit a TextView, I'm not sure if that may be preventing this from working.
// Code to scroll to label location
scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: label.frame.origin.y), animated: true)
// Code to shift view up by height of keyboard
if view.frame.origin.y == 0 {
view.frame.origin.y -= keyboardRect.height
}

UIScrollView doesn't scroll if started from pressing on textField

I have UIScrollView with labels and textFields (screenshot).
I have calculated contentViewHeight based on number of views.
When started scrolling from image or label, scrollView scrolls perfect, but if I touch from textField and drag, scrollView didn't scroll or fire scrollViewDidScroll method.
Can somebody help me with this issue?
When the textFieldShouldBeginEditing (_ :) option is trimmed, the total size of the View will be trimmed so you have those drawbacks.
You can define the scrollView in viewdidload:
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: 570)
// Do any additional setup after loading the view.
}
And using 'IQKeyboardManager' is highly recommended.