ToolBar between UINavigationBar and UITableView? - iphone

I have a UITableViewController embedded on a UINavigationController. This tableView is an instance of NSFetchedResultsController.
I need to add a Toolbar between the NavigationController's top bar and the TableViewController, but I can't figure out how to do it (I don't even know if it's possible).
I want to do something like Apple did with their WWDC App (except that they don't have the TableViewController embedded in the NavigationController).
I need to have some controls on the bar to drive the NSFetchedResultsController.
Some people suggested to people with similar problems to use a UITableView instead of a TVC, but I do need to have a TVC as an instance of NSFetchedResultsController.
Any ideas on how to accomplish this?
Would I have to do it programmatically? If so, how?
Btw, I'm targeting iOS6+ with storyboards and ARC.

The approach I prefer is to use a UIViewController at the outer level, containing your toolbar and a container view that holds the table. Then build your table in a separate UITableViewController and wire it into the container view using an embed segue. Overall, I think this makes the code more modular and easier to follow because the high-level structure is laid out in the storyboard.
The steps to use an embed segue are as follows:
Control-drag from the container view to the view controller you want to embed and select the "Embed" option.
Give the embed segue an identifier in the attributes inspector.
Configure the table view controller in the parent's prepareForSegue method, checking for your segue's identifier.
There is an example of this in my VCollectionViewGridLayout library. Take a look at the Sort & Filter example project.

Technical Note TN2154: UIScrollView And Autolayout provides another solution:
Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
That is, even if a scroll view (such as a table view) modifies the subview's frame, the auto layout engine will reset it on the next layout pass.

you need use UIViewController, then add tool bar and tableView instance of NSFetchedResultsController class inside it in storyboard

You can make a topBar of any UIView and then pass it as the tableHeaderView.
It may help you.

Use UIViewController instead of just UITableViewController where you can easily place other controls apart from just a UITableView.
Hope this helps.

You can use the UITableViewController (keep the niceties such as UIRefreshControl support and keyboard avoidance). You just have to embed your toolbar in a plain view and place that in your tableHeaderView. Then implement this scroll view delegate method to lock.
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect rect = self.toolbarContainerView.frame;
rect.origin.y = MIN(0,scrollView.contentOffset.y + scrollView.contentInset.top);
self.toolbarContainerView.frame = rect;
}
Note that if you also use section header you will have to send those views behind your tableHeaderView otherwise they will float over the tableHeaderView.

Related

Set background image of a mutated TableViewController

I am working on a swift project in Xcode right now and one of my view controllers is a UITableViewController. I used swift to alter the size of the table view in the controller so that it does not fill up the entire screen. However, I want to set a background image for the entire view controller which I am unable to do since XCode is not letting me add an image view between the table view controller and the table view. Is there any way to do it using swift? Thank you.
I think I can help out.
If you're using Apple's UITableViewController - does that mean you altered the tableView's height in the storyboard? I think a better solution is to just use a regular UIViewController, add a tableView with whatever height you want it inside a UIViewController's view.
Often times I would recommend steering away from using Apple's custom things because you lose flexibility. Just make your own custom thing instead!
UIViewController > View > TableView & UIImageView
Make sure your tableview has a clear background!

How can I place a UITableView on a UIViewController programmatically so that it doesn't take up the whole view?

I want to have a ViewController that has a table view on the top half of the screen, and a button under it. I'm assuming there's a simple way to do this. Every way I have tried so far ends up in the bottom half of the view being inaccessible.
The trick is that you cannot use a subclass of UITableViewController. You need to enable the datasource and delegate protocols explicitly in a UIViewController.
#interface MyController : UIViewController <UITableViewDatasource, UITableViewDelegate>
// variables and properties
#end
Don't forget to assign self as delegate and datasource of your tableview.
Now you can just put the table view anywhere by giving it an appropriate frame and you can also put a button at the bottom.
Hint: the standard way to "put a button at the bottom" is to use a UIToolBar. With that you could use the normal table view controller without having to worry about the protocols. But your approach is also feasible.
Well, you could have put a sample image to get the clear idea. but AFAI understood, i try to give the answer accordingly :
You can set the frame especially height of TableView just half the size of the height of your UIViewController.
Then rest of the space of UIViewCobtroller you can create another view where you can place your button as you have already done so far.
You can add the button in your UITableView's footer section.

adding a static UITableView to a regular custom view

I have a custom view controller that has a view on the bottom half.
I would like to add a static UITableView on the top half.
So I dragged a UITableView on the view controller but apparently that is not allowed since static table views only are only embeddable in UIViewControllers.
I went to my code and made my controller extend UITableView but that doesn't fix the issue.
How do I add my static UITableView as a second view in my custom controller?
EDIT: Perhaps having a table view not taking up the whole screen is not very well supported in iOS storyboards. Maybe I will just use regular tables on a view since i just need 3 static rows.
You should be able to do something simple like this:
UITableViewController *tbv = [[UITableViewController alloc] initWithFrame:[CGRect whateverSize/Location]];
[self.view addSubview:tbv];
Be sure when doing this to also write needed delegate functions such as numberOfRowsInSection: , numberOfSections: , cellForRowAtIndexPath:, tableView:didSelectRowAtIndexPath:
Be sure to check out the UITableView Class Reference
I solved this issue by making my second view controller a simple UIViewController that implements the delegate and datasource, dragging a dynamic table on it and setting the rows and sections "statically" in code. Now I have my two views correctly cohabiting in a large view.

NavigationController with search, map and table: how to

Since I am a newbie in iPhone development, I need some advice on how to structure my xib file in order to get the following (essentially, it should be very similar to Google Places app).
I need a NavigationController with:
Map with search functionality.
TableView with search functionality (ideally, the search should be shared between map and TableView).
One view to show details of a selected item, whatever the source view is (map or table).
Map view should have a button to show listing view, and viceversa.
My doubt is, what do I have to nest where? I have a NavigationController with its View Controller set to another class with its own xib, but I don't know how to go on.
Should I create a View with a search bar and another SubView to switch between Map and Table? Or is it better to have two full views each one with its own search bar?
EDIT 1
Finally I have decided to implement the following structure:
- TabBarController
- LugaresNavController (UINavigationController)
- LugaresViewController (UIViewController)
- UISearch
- SubView (UIView)
- MapViewController (UIViewController)
- TableViewController (UITableViewController)
MapViewController and TableViewController have their own xib files. What I want is to have the ability to switch between them into SubView, but I am not able to do it.
I have assigned MapViewController and TableViewController file's owner to their respective class in the xib files, and also have specified which xib to load at the attributes of their representation in the xib corresponding to LugaresViewController, which is their parent.
When I run the application, all I can see is the TabBar with the NavBar and the UISearch. The frame where SubView should render either the map or the table is showing blank...
What am I doing wrong here?
Your fundamental problem is that you are getting your different parent-child hierarchies confused.
To recap: MVC is Model, View and Controller.
iOS has TWO different parent-child hierarchies: One for the controllers and one for the views.
The following are part of the controller parent-child hierarchy:
- TabBarController
- LugaresNavController (UINavigationController)
- LugaresViewController (UIViewController)
This means that LugaresViewController.parentViewController should be LugaresNavController and so on.
However the following doesn't make any sense:
- SubView (UIView)
- MapViewController (UIViewController)
- TableViewController (UITableViewController)
Views can have subviews but views can't have subviewcontrollers,
Are you doing the following?
- SubView (UIView)
- MapViewController.view (UIViewController s view is a subview of Subview)
- TableViewController.view (UITableViewController s view is a subview of Subview)
This is a view parent-child hierarchy. This means that:
SubView has two subviews (MapViewController.view and TableViewController.view)
MapViewController.parentViewController is nil.
MapViewController.navigationController is nil.
you can use view transtions to switch between MapViewController.view & TableViewController.view but not viewController transitions (such as presentModal... or push or pop ViewController...).
If this is what you want, then you just have to make sure that you do:
[SubView addSubview: MapViewController.view];
[SubView addSubview: TableViewController.view];
Incidentally, SubView is a terrible name. Usually we call those containerView or something like that.
subview is such an important concept you want to be able to diffrentiate the concept of a subview from the instance of that view. It's like naming your child "Kid" or your dog "Puppy".
your App is similar to mine, what I've done:
- TabBarController
- TableView
- SearchBar
- DetailButton ----\
- MapView \____ push ModalViewController
- SearchBar /
- DetailButton ----/
now if someone pushes a button in the table view or map view I'm opening the same View but pass other information to initialize it. btw. what is a LugaresViewController?
First try only adding a TableView and a MapView in each tab and look if the nib-binding works well. If not, you're missing something, load nib named or class type. Afterwards you can add the SearchBarController's and provide functionality for that.
First of all, I can't really say I agree with your tabbar application. I think a navigation application might work better for this idea. The first view would then have the navigationbar automatically added, with below the UISearchbar, a window, and a normal tabbar.
The UIWindow should contain a subview. That would be either the table, or the map. When you first load the application, and show, say, the table, you'd do:
[window addSubview:tableViewController];
When you switch to the mapview, you'd do something like:
[tableViewController removeFromSuperview];
[window addSubview:mapViewController];
So, for switching views, that's where the UITabBar comes into play. Make sure the tabbar has two segments. Hook up an IBAction to that, which looks a bit like:
-(IBAction)segmentedControlIndexChanged {
switch (segmentedControl.selectedSegmentIndex} {
case 0:
<call function for removing mapview and adding tableview>
case 1:
<call function for removing tableview and adding mapview>
default:
break;
}
}
Last but definately not least, which might be the answer to your edit: make sure you've connected all the connectors in Interface Builder (or, if your using XCode 4, just the .xib-file), from the .xib to the File Owner's.
EDIT: Ofcourse, the detail-view for the items pressed on the table/map is pushed in the navigationItem, so the back-button is automatically created and functional in your detailview.
Good luck!

How to programmatically adda breadcrumb view to my UINavigationController App?

I have a UINavigationController App. I want to add a small bar just below the UINavigationBar, around 20px height. y application is almost finished, so I want to rebuild as less code as possible. For example, if I wanted to add a button in the bottom of every view of my application, I can do that by extending UIViewController with a category, and adding a UIButton as a subview of the current controller view, maybe in the viewDidLoad method.
This approach works fine, and so I can add my UILabel to all my views at the top of them. The problem is that it does not TAKE SPACE. It is always on top of my previous views (UITableView...). What is the best way (or just one way) to accomplish such a thing without having to create for example a view with 2 frames, and having all my main views extending it?
I thought of changing UINavigationBar height, but that is definitely not an option.The prompt property of UINabivationBar is just to big (around 30px).
I also tried to create a new view in the viewWillAppear method of every UIViewController, adding to that view my breadcrumb subview, and the original view, but it is not working.
Any ideas on this?
Thank you!
If I were you, I'd make a new UIView subclass to represent this thing, and embed it on the views of the individual UIViewControllers. They can get at the navigation stack by looking at the UINavigationController's .navigationControllers array, walk that and get view titles, etc.