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
Related
I have a Navigation Controller and a Collection View under it inside my app. And there is a problem: I use large title inside my Navigation bar, so everything inside is not static. When I scroll the collection view cells, the title (I created it manually using UILabel() to move it as I want inside the navigation bar) and buttons move up and the navigation bar takes form of iOS 10 navigation bar, I mean its height. You can see it here:
The normal state of my Navigation Bar with "Prefer large titles" On:
It happens when I scroll my Collection View, everything goes up:
So the question is simple: how to make the force constant height for the navigation bar? I want it to become fixed even while scrolling. Are there any ideas? Is it possible?
And the second question, if the first is impossible: Another solution for my problem is to make the Navigation Bar with "Prefer large titles" Off bigger. I tried this code:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
but it worked only for large titles. So how can I make the navigation bar bigger?
Yes, you can make it fixed. It will not scroll if the very first view in the view hierarchy is not a CollectionView/TableView (ScrollView).
Using Storyboard/Xib:
Consider the following image where a tableView and button are added in scene. Here the navigation bar will collapse on scroll of tableView because tableView is the very first view in viewController's containerView hierarchy attached to the navigation bar.
Now to make the navigation bar fixed, if we just change the order of tableView and button as below, it will disable the collapsing of navigation bar.
To change the order of the view, you have to click, hold and move up/down.
If you have only CollectionView in this scene then you can add a placeholder view at the top and set its height to zero as below,
Programmatically:
If you are setting up view's programmatically then you just need to add a placeholder view at the top or add tableView/collection after adding other views.
e.g,
self.view.addSubview(UIView(frame: .zero))
self.view.addSubview(tableView) // or collectionView
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 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 ?
I'm working on a UITableViewController. I have to display a view with some info, that view will be added to the TableViewController programatically. This is my code of adding it:
MyNotificationView *notificationView = // some initializations;
[self.view addSubview:notificationView]; // self is the UITableViewController
notificationView.center = self.view.center;
However on UITableViewController, this code actually sets the center of my notificationView to the center of the scrollable area of the TableViewController, and my notificationView scrolls as the TableView scrolls.
What I want is to add my notificationView to the screen center and stick it there, so how can this be done?
You can make a UIViewController with an UITableView inside and the notificationView you want to keep in center, just add it to view [self.view addSubview:notificationView];
In my view controller, I have an UIImageView with a frame of (0, 0, and a hidden UITableView with a UISearchBar on top. The tableview has a frame of (0, 20, 320, 548) to show the imageview under the status bar whenever the tableview is shown. However, when I scroll the cells up past the search bar, the cells go over the imageview but under the status bar text.
Why is this happening when the tableview's frame starts at (0, 20)? How can I make the imageview show under the status bar when the tableview is shown, without having the cells block it?
Edit:
I started with a ViewController created in storyboard. Both the UIImageView and UITableView are subviews of the main view. The button's outlet is called searchBarLikeButton.
- (IBAction)searchBarLikeAction:(id)sender {
//Hide button, show table, make searchBar the first responder.
self.searchBarLikeButton.hidden = YES;
self.carTableView.hidden = NO;
[self.searchBarByName becomeFirstResponder];
}