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
}
}
Related
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
}
}
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
}
}
}
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.
I want to stop backward scrolling on ScrollView after user scrolls to the next page. How can I do that.
I tried the following two codes, but the first one does not have any effect
self.scrollView.contentOffset = CGPointMake(self.view.frame.width,0)
and the second only disables the forward scrolling.
self.scrollView.contentSize = CGSizeMake( 2 * scrollWidth, scrollHeight);
To disable scrolling in one direction you implement the UIScrollViewDelegate method scrollViewDidScroll and put your logic there. For instance this TableViewController can only ever scroll down, because if the user tries to scroll up, we just overwrite the contentOffset, effectively undoing their scroll before they see it.
class ViewController: UITableViewController {
var lastScrollPosition = CGPoint.zero
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView.contentOffset.y > lastScrollPosition.y else {
scrollView.setContentOffset(lastScrollPosition, animated: false)
return
}
lastScrollPosition = scrollView.contentOffset
}
}
If your cell is equal in size to your screen, you can apply the following option, which is very smooth:
var lastScrollPosition = CGPoint.zero
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x == lastScrollPosition.x + UIScreen.main.bounds.width {
lastScrollPosition.x += UIScreen.main.bounds.width
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView.contentOffset.x > lastScrollPosition.x else {
scrollView.setContentOffset(lastScrollPosition, animated: false)
return
}
}
I have created a UIScrollView in a ContainerView and a UITextView as a subview of UIScrollView. I initialize the textView with scrollEnabled set to true. Now based on the contentOffset.y of the scrollView, I want to keep toggling scrollEnabled on the textView. But for some reason, once I set scrollEnabled to false, I can't seem to enable scroll again...
override func viewDidload() {
self.scrollView = UIScrollView(frame: CGRectMake(...))
self.scrollView.delegate = self
self.view.addSubview(self.scrollView)
self.textView = UITextView(frame: CGRectMake(...))
self.textView.scrollEnabled = true
self.textView.userInteractionEnabled = true
self.scrollView.addSubview(self.textView)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if self.scrollView.contentOffset.y >= 180 {
self.textView.scrollEnabled = true // This does not work!
} else {
self.textView.scrollEnabled = false // This works!
}
}
I was faced with the same problem, and eventually dealt with it by subclassing UITextView as shown below:
class TextView: UITextView {
var displayScrolling: Bool = true {
didSet {
self.showsVerticalScrollIndicator = displayScrolling
}
}
override var contentOffset: CGPoint {
set {
if displayScrolling {
super.contentOffset = newValue
}
}
get {
return super.contentOffset
}
}
}
At init (or in interface builder), set your instance of TextView to allow scrolling. In order to not scroll, set the 'displayScrolling' var to false. When you want to scroll, set 'displayScrolling' to true.
It will be scroll enabled if the text is longer.
I wish you luck!