How hide and show view when scroll page - ios

I wrote this code, and i hided view
self.tableView.tableHeaderView = nil
But if i try show it again, this code cant do it
tableView.tableHeaderView = tableHeaderView
Help me please
My code
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset < scrollView.contentOffset.y) {
// moved to top
self.tableView.tableHeaderView = nil
}
else if (self.lastContentOffset > scrollView.contentOffset.y) {
if tableView.tableHeaderView == nil {
// moved to bottom
tableView.tableHeaderView = pre_test_view
}
}
else {
// didn't move
}
}

So when you initially instantiate the UITableView and set the tableViewHeader to nil you are not really hiding it...so much as initiating the tableView without it. What you can do..is call tableView.reloadData() after tableView.tableHeaderView = tableHeaderView and that should probably do it.

Related

How to hide/show a uiview when uiscrollview Y position changes?

I have a uiview in my app. I want to show the uiview when uiscrollview Y position changes. I tried some stackoverflow suggestion but can't get the expected result.
Here's my code that i tried:
if (topScrollView.contentOffset.y) >= 30{
slideInSearchView.isHidden = false
} else {
slideInSearchView.isHidden = true
}
Make sure code is inside scrollViewDidScroll
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (topScrollView.contentOffset.y) >= 30{
slideInSearchView.isHidden = false
} else {
slideInSearchView.isHidden = true
}
}

equalize the position of the tableView scroll in swift

I have a case, there are 2 tableView in 1 UIViewController, how to do when tableView1 is scrolled The position of scroll tableView2 also follows. thank you
You can try
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.tableView1 == scrollView {
self.tableView2.contentOffset = scrollView.contentOffset
}
else {
self.tableView1.contentOffset = scrollView.contentOffset
}
}

How to show / hide custom view depending the contentOffset of my tableview?

I have a chat app and I'm trying to show a custom view that I've made when the user scrolls to the top, also hide it if it's on the bottom of tableview. (like whatsapp does it)
To be honest I'm struggling with the logic of show/hide button.
Tried to save the contentOffset.y of my tableview right after I reload the data so I'll know that's the bottom, and if it's smaller to show the custom view, but mainTableView.contentOffset.y it's always 0.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if (scrollView == mainTableView) {
print(mainTableView.contentOffset.y)
if let point = startingPointForView {
//where var startingPointForView: CGFloat?
// and tried to save it after I reload the data
//self.startingPointForView = self.mainTableView.contentOffset.y
// but it's always 0
}
// Show and hide button logic
}
}
An image of what I m trying to achieve: https://imgur.com/ZkYEi2P
try this code to hide/show custom view according to UIscrollview contentOffset
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let scrollViewContentHeight = scrollView.contentSize.height
let scrollViewHeight = scrollView.frame.height
if scrollView.contentOffset.y < (scrollViewContentHeight - scrollViewHeight){
//Custom view show
}else{
//Custom view Hide
}
}
May be this code will help you
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y > 0 {
// down
button.isHidden = false
} else {
// up
button.isHidden = true
}
}
For someone who is looking to hide a button when tableview is scrolling can use below code:
var previousContentOffset: CGFloat = CGFloat()
extension YourViewController: UIScrollViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.yourTableView{
let currentContentOffset = scrollView.contentOffset.y
if (currentContentOffset > previousContentOffset) {
// scrolling towards the bottom
if scrollView.contentOffset.y > 50 {
self.yourButton.isHidden = true
} else {
self.yourButton.isHidden = false
}
} else if (currentContentOffset < previousContentOffset) {
// scrolling towards the top
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
// Change 10.0 to adjust the distance from bottom
if maximumOffset - currentContentOffset <= 10.0 {
self.yourButton.isHidden = true
} else {
self.yourButton.isHidden = false
}
}
previousContentOffset = currentContentOffset
}
}
}

Detect when Scrollview Scrolling not tableview scrolling

I have a scrollview inside tableview (tableview's header) and I want to call this action when scroll finish:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageNumber = round(bookScrollView.contentOffset.x / bookScrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
print("scroll")
}
when I scroll bookScrollView nothing happen, but when table view scroll this function run.
How I detect when bookScrollView scroll and run function?
thanks
Here, you can define, scrollOfTable, or you can also define TAG property. identify and only scroll when scrollview comes in, not tableview.
You also need to check, if your scrollView is setting delegate
self.vsScrollView.delegate = self
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView == scrollOfTable {
}
else if scrollView == scrollOfScrollView {
}
else {
//something else
}
}

keep the UISearchBarDisplayController on Top of tableview

i'm trying to keep the UISearchBarDisplayController on top of UItableview in swift
i tried this code (which i convert from objectC to swift) but no luck:
override func scrollViewDidScroll(scrollView: UIScrollView) {
//
var searchBar: UISearchBar = (self.searchDisplayController?.searchBar)!
//
var rect = searchBar.frame
self.searchDisplayController?.searchBar.frame = CGRectMake(0, max(0, scrollView.contentOffset.y), rect.width, rect.height);
}
the search bar still scroll with tableview when i scroll the table. Does anyone can help me to fix it? thanks
i found that i have to make a new view controller , add a tableview and add a searchBar outside of the tableview. and the search bar won't scroll with the tableview
In Swift 2.1, iOS 9.2.1 and Xcode 7.2
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
/* Search controller parameters */
searchController.searchResultsUpdater = self // This protocol allows your class to be informed as text changes within the UISearchBar.
searchController.dimsBackgroundDuringPresentation = false // In this instance,using current view to show the results, so do not want to dim current view.
definesPresentationContext = true // ensure that the search bar does not remain on the screen if the user navigates to another view controller while the UISearchController is active.
let tableHeaderView: UIView = UIView.init(frame: searchController.searchBar.frame)
tableHeaderView.addSubview(searchController.searchBar)
self.tableView.tableHeaderView = tableHeaderView
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
let searchBar:UISearchBar = searchController.searchBar
var searchBarFrame:CGRect = searchBar.frame
if searchController.active {
searchBarFrame.origin.y = 10
}
else {
searchBarFrame.origin.y = max(0, scrollView.contentOffset.y + scrollView.contentInset.top)
}
searchController.searchBar.frame = searchBarFrame
}

Resources