I want to hide UIView while scrolling down and showup when scrolling up.
So I have a tableView and I would like to hide the UIView above it as the user scrolls down and showup when scroll up (here is example below):
UIView below navigation controller+Embedded Segmented Controller in the UIView.
Should I use: scrollViewDidScroll ?
Related
I have a UIViewController with following:
UIView as navigation (Button, Label, Button) Height: 60
UIView with Information (Labels) height: 110
UITableView
I would like when the user scroll up, the 2nd UIView right above the UITableVIew to move with it and move off the screen, like this:
.
Finally, when user scroll down again, the UIView will appear again back to initial screen.
Delete your 2nd information view from ViewController and set it as a header view of your tableView
tableView.tableHeaderView = informationView
What is the best approach for attaching a UIButton on top of UIScrollView or UITableView so when the view is scrolled, the button stays in its place.
Here examples below:
UIButton stays in the right bottom corner when the view is scrolled.
google+ app example
yahoo mail app example
I think this should work. Lay Out your button in a view that is outside of the tableviewcontroller. Then drag an outlet to the tableviewcontroller file. Then add it in code. This code would hold it at the top of the screen.
#IBOutlet var buttonView: UIView!
override func viewDidLayoutSubviews() {
self.view.addSubview(buttonView)
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
var rect = self.buttonView.frame
rect.origin.y = max(0,scrollView.contentOffset.y + scrollView.contentInset.top)
self.buttonView.frame = rect
}
Thank you all for great answers!
I got it worked through storyboard by moving the button from scrollView to View itself. That way it's attached on UIView and it's independent of scrollview.
storyboard snapshot
So now the structure is:
- View
- ScrollView
- Button
Before it was:
- View
- ScrollView
- Button
There are many ways to go about doing this but two that I use most often are as follows.
One approach is embedding the view controller within a navigation controller. This will set a bar on the top and bottom if you choose that you can place bar button items upon.
Another approach is to place a UIView along the top and snap the constraints to the left, right, and top with 0 no-margin. Then set the height. I usually use 40px for the height but you can use what is applicable to your needs. After that you can place a button in that UIView and then set constraints on it to keep in in place.
In my experience, this isn't reliably possible to do with the scrollView itself.
My solution is usually to put anything that needs to float above the tableView/scrollView in a plain ViewController that also contains the tableView/scrollView parent.
If you're using storyboards with a UITableViewController scene, this will likely mean you need to use another scene with UIViewController with a container that has your UITableViewController.
For UITableView use tableHeaderView. For UIScrollView you need to create a separate view not in the scroll view's hierarchy.
Another solution is to put your UIButton in a UIToolbar, and then make the toolbar a child of the UINavigationController's view. After that, in viewDidLayoutSubviews, you can set the rect of the toolbar to sit just below the navigation bar and offset the top of the UIScrollView or UITableView.
Add button which you want in the storyboard.
Design your scrollview
self.view.sendSubviewToBack(scrollViewObj)(in the code)
This worked for me.
I have a UIScrollView that contains a bunch of view controllers, and in each view controller there is a button that if pressed, should scroll the UIScrollView to a certain position. How do I go about connecting the viewController IBAction for the button to set the scroll position of the UIScrollView?
I know I'll call contentOffset at some point, but I'm struggling with getting the ViewControllers to control the Scroll View.
One way you could do it... When you add each UIViewController.view to the UIScrollView you could also set a var in each UIViewController that references the UIScrollView.
For example, for some UIViewController, we'll call it X, put
var scrollView: UIScrollView?
in it. Then in your class that controls the UIScrollView, when you add view X to the UIScrollView, also do the following:
viewControllerX.scrollView = self.theScrollView
Now, when your IBAction method is called in viewControllerX, you have a reference to the UIScrollView and as you said, can do self.scrollView.contentOffset.y = someValueHere to change its position.
On my controller I have two UICollectionView instances and one UITableView instance. When tableView is on top of view hierarchy and I tap on status bar it does not scroll to top.
I've tried to set scrollsToTop property for collectionViews to NO. But tableView still doesn't scroll to top. Even more: collectionViews still scroll when I tap status bar.
I've gone further. I created a category on UIScrollView, where I swizzled -didMoveToWindow method like this:
- (void)swizzled_didMoveToWindow
{
[self swizzled_didMoveToWindow];
self.scrollsToTop = NO;
}
And it didn't work too! Scroll to top by tapping on status bar still works for all UICollectionView instances!
Please, tell me, what am I doing wrong?
If I have an element in a scroll view(not positioned in the top) how would I be able to let this element stay right under the navigation bar when it hits the bar on scroll ? Like the shuffle bar in the spotify app ?
In your ScrollView delegate:
Implement viewDidScroll:
Check the offset of the scroll view in this method: adjust the position of your nav bar using the negative offset of the scrollview: self.navBar.frame.origin.y = - view.yOffset;