Reputation: 71
I have been working on a weather app for several weeks now.I currently have 4 view controllers setup and functioning. Everything is currently linked to my tab bar controller however I need more space on the bar and 3 of the view controllers are similar. I'm not sure what the best way to do this is.The 3 like view controllers are forecast controllers, one is a current conditions, one is a extended forecast and the third is a weekend view. what I would like to do is have the current controller linked to the tab bar (easy enough) and have the option to switch the views to the other options from the current view. I tried adding another tab bar controller but I didn't like the way it looked with two tab bar controllers. I added a picture so you can see how it is currently setup, basically I want to elliminate the last 2 tabs and access them through the 2nd tab. Any suggestions on the best most effective method to accomplish this goal?
Upvotes: 2
Views: 370
Reputation: 385600
This is really more of a UI design question than a programming question… but I will put some programming in anyway.
I suggest you put the three forecasts in a single, paging-enabled scroll view. Then you just need one tab, and you can let the user swipe between the forecasts:
You can also implement this scrolling with a UIPageViewController
on iOS 6 by setting its transitionStyle
to UIPageViewControllerTransitionStyleScroll
.
To make the user aware that there are multiple forecasts available, you could add a UIPageControl
, if you can find room for it on that rather busy forecast screen.
Another way to make the user aware of the other forecast pages is to bounce the scroll view when it appears. This is the programming comes in!
Do this in the forecast tab's view controller:
- (void)viewDidAppear:(BOOL)animated {
[self bounceScrollViewIfNeeded];
}
- (void)bounceScrollViewIfNeeded {
if (scrollView_.contentOffset.x != 0)
return;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
scrollView_.contentOffset = CGPointMake(20, 0);
} completion:^(BOOL finished) {
if (finished && !scrollView_.tracking) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
scrollView_.contentOffset = CGPointZero;
} completion:nil];
}
}];
}
This code makes the scroll view bounce a little when the forecast tab appears, if the scroll view is showing the leftmost page. You might also want to stop doing the bounce after the user has seen it a few times. Anyway, it looks like this:
If the user tries to start dragging while the bounce is happening, you need to cancel the animations. You can do that by assigning your view controller as the scroll view's delegate, and implementing this delegate method:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[scrollView_.layer removeAnimationForKey:@"bounds"];
}
I don't think there's an easy way to do a bounce with a UIPageViewController
.
Upvotes: 4
Reputation: 5581
I would look into using a UISegmentedControl
at the top of the view. If the views are similar enough that they can be displayed with only minor variation using the same UIViewController
class, this is an easy solution.
If the views differ enough that it would be inconvenient to control the views in the same UIViewController
, I like to use container views, although they are only available in iOS 6. You can embed a UIViewController
using an embed segue into the container view's UIView
. In the segmentedControlValueChanged:
method, just hide the container views for the other segments and unhide the container view that is currently selected.
Upvotes: 1