Scroll to top in scrollview when click on any button in swift - 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

Related

How to scroll to button's position in scrollView

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)

UICollectionView not scrolling after custom UIView added to view

I'm trying to add a custom UIView after adding a UICollectionView because I wanna show the custom bar view above the collection view. This is my code:
func loadFilters() {
let categoriesFlowLayout = UICollectionViewFlowLayout()
categoriesFlowLayout.scrollDirection = .vertical
categoriesCollection = UICollectionView(frame: CGRect(x: 10, y: getY(yAxis: searchField) + 10, width: view.frame.size.width - 20, height: (view.frame.size.height * 9 / 10) - getY(yAxis: searchField) - 10), collectionViewLayout: categoriesFlowLayout)
categoriesCollection.register(categoriesCell.self, forCellWithReuseIdentifier: "categoriesCell")
categoriesCollection.delegate = self
categoriesCollection.dataSource = self
categoriesCollection.backgroundColor = UIColor.clear
categoriesCollection.showsVerticalScrollIndicator = false
categoriesCollection.showsHorizontalScrollIndicator = false
view.addSubview(categoriesCollection)
addBar()
categoriesCollection.isUserInteractionEnabled = true
}
The addBar() function is declared in the custom superclass ViewController
if addBar() is called before view.addSubview(categoriesCollection) it looks like the image below but if it is called after then my collection view does not scroll or recognize touches. Is there anyway that will make the collection view scroll and bring the custom bar to front?
I've used sendSubviewToBack() and bringSubviewToFront() functions as well but the result is the same
When you insert a view above another view, the top view gets all touch events. So the UICollectionView does not receive any touch events anymore since another view is above it.
As I see from your post, you just want the bar at the bottom of the screen. So check the size of your custom UIView. It probably fills the entire screen and is completely above the UICollectionView. Just give the UIView some sort of background color and see, how much space it fills.
If this doesn't work, you can use a UIGestureRecognizer on the custom UIView and forward the touch events to the UICollectionView.

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
}

UITableview scrollToRow function error when view is added to tableview

I added a view to tableView by using addSubview
let view = UIView(frame: CGRect(x: 52, y: ypos + 25, width: self.view.frame.width - 52, height: 1.5))
timeIndicatorView = view
self.eventCatalogTableView.addSubview(timeIndicatorView)
and then I use scrollToRow function on the tableView to scroll to the current Time.
func rollToCurrentTime() {
eventCatalogTableView.scrollToRow(at: IndexPath(row: Calendar.current.component(.hour, from: Date()), section: 0), at: UITableViewScrollPosition.middle, animated: false)
}
Everything works normally when I scroll to the row where the added view cannot be seen. Like this
However, when I scroll to the row where the added view is showed, the position of the view changes to a weird random position like this
In this case, when I scroll all the way up to the top and then scroll down, the view gets positioned in the right spot.
Is this an internal bug or is there any way to fix this issue?

How can I move view's content up when iAd banner appears? (programatically in Swift)

I am adding an iAdBanner to a view controller programatically but have run into this problem: text (label in scroll view) covers the ad
I want the content of the view controller to shift up as to not cover the add, and I want to move the banner so it is on top of the scroll view as it is currently behind and can not be tapped.
I am trying to do this programatically as I have tried already in interface builder and keep running into problems such as displaying two ads after segueing out etc.
Any help would be appreciated
Here is my code so far:
var bannerView: ADBannerView!
bannerView = ADBannerView(adType: .Banner)
bannerView.delegate = self
bannerView.center = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height - bannerView.frame.size.height / 2)
view.addSubview(bannerView)
This is just from top of my head, but assuming you have var scrollView, you should be able to adjust it's frame(size of frame in which content appears) like this:
scrollView.frame.height = view.frame.height - bannerView.frame.size.height