Show tab bar after its hidden - iphone

Is there any way to show a tab bar after it has been hidden?
Got a tabbar-nav structure. For one of the tabs, I need to hide the tab bar for its 2nd and 3rd level view. But at the same time I will need to show its 1st and 4th view.
The sample code from Elements isn't really applicable here I think.

I've found quite a good pragmatic solution to this problem - make the UITabBarController's view larger than it needs to be, so that the actual UITabBar is clipped by the screen.
Assuming that the tab bar view normally fills its superview, this sort of thing should work:
CGRect frame = self.tabBarController.view.superview.frame;
if (isHidden)
{
CGFloat offset = self.tabBarController.tabBar.frame.size.height;
frame.size.height += offset;
}
self.tabBarController.view.frame = frame;
The tab bar is still showing, but it's off the bottom of the screen, so appears to have been hidden.
It might have performance implications if it causes extra clipping, but so far, it seems to work.

The UIViewControllers that are pushed onto the navigation stack can do the something like the following:
- (void)viewWillAppear:(BOOL)animated {
self.tabBarController.tabBar.hidden = NO; // Or YES as desired.
}
EDIT: Added additional code below to deal with the frame. Don't think I particular recommend this idea since it relies on the internal default view structure of a UITabBarController.
Define the following category on UITabBarController:
#interface UITabBarController (Extras)
- (void)showTabBar:(BOOL)show;
#end
#implementation UITabBarController (Extras)
- (void)showTabBar:(BOOL)show {
UITabBar* tabBar = self.tabBar;
if (show != tabBar.hidden)
return;
// This relies on the fact that the content view is the first subview
// in a UITabBarController's normal view, and so is fragile in the face
// of updates to UIKit.
UIView* subview = [self.view.subviews objectAtIndex:0];
CGRect frame = subview.frame;
if (show) {
frame.size.height -= tabBar.frame.size.height;
} else {
frame.size.height += tabBar.frame.size.height;
}
subview.frame = frame;
tabBar.hidden = !show;
}
#end
Then, instead of using the tabBar.hidden change I originally suggested, do the following:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tabBarController showTabBar:NO];
}
Obviously making sure that the implementation has included the category definition so that 'showTabBar' is known.

You need to implement a delegate method
- (BOOL)tabBarController:(UITabBarController *)tabBarController2 shouldSelectViewController:(UIViewController *)viewController
Inside that you can check which index is selected and show the tab bar
if([[tabBarController.viewControllers objectAtIndex:0] isEqual:viewController])// it is first tab
{
tabBarController.tabBar.hidden = FALSE;
}

I know this is an old post but i think the below code would help to hide the tabbar on the viewcontroller you don't want it on and has the added benefit of automatically readding the tabbar when you come back from that view controller
UIViewController *hideTabbarViewController = [[UIViewController alloc] init];
hideTabbarViewController.hidesBottomBarWhenPushed = YES;
[[self navigationController] hideTabbarViewController animated:YES];

Related

iOS 7 Table view fail to auto adjust content inset

I am transiting my project to iOS7. I am facing a strange problem related to the translucent navigation bar.
I have a view controller and it has a tableview as subview (let's call it ControllerA) . I init a new uinavigationcontroller with the controllerA and present it modally using presentviewcontroller. The presented view controller's table view is blocked by the navigation bar. I set the automaticallyAdjustsScrollViewInsets to YES but the result did not change.
I knew I can set the edgesForExtendedLayout to UIRectEdgeNone, but it will make the navigation bar no more translucent.
After that, I tried to create a new view controller for testing. It contains almost the same elements. But the result is much different. The table view content does not get blocked.
Conclusion
Two View Controllers' automaticallyAdjustsScrollViewInsets set to YES
The project is not using storyboard
The first one is created at Xcode 4.6, The second one is newly created on Xcode 5
I have compared two classes xib and code, not much different
I have found the answer on apple developer forum.
There are two different case.
The first one, the view controller added is a UITableViewController.
And the issue should not be appeared since apple will auto padding it.
The second one, the view controller is NOT a UITableViewController.
And in the view hierarchy, it contains a UITableView. In this case, if the UITableview(or ScrollView) is the viewController's mainview or the first subview of the mainview, it will work. Otherwise, the view controller doesn't know which scroll view to padding and it will happen the issue.
In my case, the view controller is the second one. And there is a background image view as the first subview of the main view. So, it fails.
Here is the Apple developer forum link (need developer account to access):
https://devforums.apple.com/message/900138#900138
If you want the view to underlap the navigation bar, but also want it positioned so the top of the scrollview's content is positioned below the navigation bar by default, you can add a top inset manually once the view is laid out. This is essentially what the view layout system does when the top-level view is a scroll view.
-(void)viewDidLayoutSubviews {
if ([self respondsToSelector:#selector(topLayoutGuide)]) {
UIEdgeInsets currentInsets = self.scrollView.contentInset;
self.scrollView.contentInset = (UIEdgeInsets){
.top = self.topLayoutGuide.length,
.bottom = currentInsets.bottom,
.left = currentInsets.left,
.right = currentInsets.right
};
}
}
Based on Tony's answer I was able to get around this problem programatically with temporarily sending the table view to the back, let the adjustments be made and then send the background view back to the back. In my case there is no flickering to this approach.
In the View Controller:
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self.view sendSubviewToBack:self.tableView];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view sendSubviewToBack:self.backgroundView];
}
Obviously if there are other subviews on self.view you may need to re-order those too.
There's probably too many answers on this already, but I had to take Christopher's solution and modify it slightly to support view resizing and allowing the content inset to be changed in a subclass of the UIViewController.
#interface MyViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (assign, nonatomic) UIEdgeInsets scrollViewInitialContentInset;
#end
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setScrollViewInitialContentInset:UIEdgeInsetsZero];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
if (UIEdgeInsetsEqualToEdgeInsets([self scrollViewInitialContentInset], UIEdgeInsetsZero)) {
[self setScrollViewInitialContentInset:[self.scrollView contentInset]];
}
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIEdgeInsets scrollViewInset = [self scrollViewInitialContentInset];
if (UIEdgeInsetsEqualToEdgeInsets(scrollViewInset, UIEdgeInsetsZero) {
if ([self respondsToSelector:#selector(topLayoutGuide)]) {
scrollViewInset.top = [self.topLayoutGuide length];
}
if ([self respondsToSelector:#selector(bottomLayoutGuide)]) {
scrollViewInset.bottom = [self.bottomLayoutGuide length];
}
[self.scrollView setContentInset:scrollViewInset];
}
}
#end
To explain the point:
Any subclass of MyViewController can now modify the contentInset of scrollView in viewDidLoad and it will be respected. However, if the contentInset of scrollView is UIEdgeInsetsZero: it will be expanded to topLayoutGuide and bottomLayoutGuide.
#Christopher Pickslay solution in Swift 2:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let topInset = topLayoutGuide.length
inTableView.contentInset.top = topInset
inTableView.contentOffset.y = -topInset
inTableView.scrollIndicatorInsets.top = topInset
}
Yeah - a bit annoying.
I have a nib with a single tableview within the main view, not using autolayout. There is a tabbar, navigationbar and a statusbar and the app needs to work back to 5.0. In Interface builder that neat 'see it in iOS7 and iOS6.1 side-by-side' thing works, showing the tables neatly fitting (once the iOS6/7 deltas were set properly).
However running on a device or simulator there was a large gap at the top of the table, which was as a result of a content inset (which pretty much matched by iOS6/7 vertical delta) that was set to zero in the nib.
Only solution I got was in viewWillAppear to put in [_tableView setContentInset:UIEdgeInsetsZero].
Another ugly hack with a pretty on-screen result.....
[self.navigationController setNavigationBarHidden:YES animated:YES];
in:
- (void)viewDidLoad

How can I create one UIViewController with two views that display one or the other depending on button clicked

I'm quite new to iOS development and I am stuck. Currently I am using one tab controller to switch between two view controllers (list and map view). This made it easier to use storyboard to configure the look of the two views.
Now the requirements have changed and the app needs to have one view controller with a segmented control that on click, displays either the list or the map view. In order to do this, I would need to make one view controller that can display list/map view.
I understand how the segmented controller part works, but I'm just stuck on how I can go about having two views with one or the other displayed in the same area.
How can I go about having two views in one view controller (if possible, utilizing storyboard)?
Thanks in advance!
You should not have two main views in a single view controller, instead you need to create one view controller per view that you want to show. However you can certainly have multiple subviews in a single view controller, which may be what works for you.
There are a number of approaches to solve this the problem, the correct approach would be to create a container UIViewController, and add as its childs the 2 viewcontrollers you want to show, them simply set the view to the view controller you want to display, but that would probably be overly complicated since you mention you are new to iOS development.
Therefore an easy solution (not sure if you can implement this in storyboard - since I don't like it), would be to have a single view controller, with the tabs, and 2 subviews of the main view, then you can simply switch between views by doing something like this:
[self.view addSubview:view1];
//to switch
[view1 removeFromSuperview];
[self.view addSubView:view2];
alternatively, you do not really need to remove it from superview but just hide it, and then use bringSubViewToFront to show the view that you need.
If you want to use the other approach I would recommend looking for this video the WWDC 2011 video titled "Implementing UIViewController Containment". This other question should be useful to: UISegmented control with 2 views
Hope that helps.
Using storyboard api you can switch between 2 viewControllers
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *viewController = [self viewControllerForSegmentIndex:self.typeSegmentedControl.selectedSegmentIndex];
[self addChildViewController:viewController];
viewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:viewController.view];
self.currentViewController = viewController;
}
- (UIViewController *)viewControllerForSegmentIndex:(NSInteger)index {
UIViewController *viewController;
switch (index) {
case 0:
viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
break;
case 1:
viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
break;
}
return viewController;
}
- (IBAction)segmentChanged:(UISegmentedControl *)sender {
UIViewController *viewController = [self viewControllerForSegmentIndex:sender.selectedSegmentIndex];
[self addChildViewController:viewController];
[self transitionFromViewController:self.currentViewController toViewController:viewController duration:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
[self.currentViewController.view removeFromSuperview];
viewController.view.frame = self.contentView.bounds;
[self.contentView addSubview:viewController.view];
} completion:^(BOOL finished) {
[viewController didMoveToParentViewController:self];
[self.currentViewController removeFromParentViewController];
self.currentViewController = viewController ;
}];
self.navigationItem.title = viewController.title;
}
This is in reference to iOS tutorial by Raywenderlich. Hope this helps
With Storyboard it is possible in this way.
Create UIViewController with UISegmentControl and UITableView+UITableViewCell added to it.
Now you want to add MKMapView as well, hoverer, if you simply try to place the MapView on the ViewController, it will be added as new TableView cell, which is not what we want.
That's why you should not do it so. Instead, MapView has to be added to Storyboard's List of ViewControllers
Adjust the size and origin of MapView to be the same as TableView ones.
Now, setHidden to YES for either TableView of MapView, create and synthesize outlets for them. Then in Segment control Value Changed method implement switching:
- (IBAction)switchView:(id)sender {
self.theTableView.hidden = !self.theTableView.hidden;
self.theMapView.hidden = !self.theMapView.hidden;
if (!self.theTableView.hidden) {
[self.theTableView reloadData];
}
}

How to replace UITableView within UIViewController on selection

I am coding in xCode 4.3
This is my first application.
I have a UIViewController, company logo on top, then search bar and then UITableView in the middle (with product names) and bottom footer image. Now I want that once an item is clicked on UITableView, only TableView is replace with a view showing product details.
Right now I can replace the entire view with following code:
if (!self.prodDetailViewController_)
{
self.prodDetailViewController_ = [[PCS1ProdDetailViewController alloc] initWithNibName:#"PCS1ProdDetailViewController" bundle:[NSBundle mainBundle]];
}
[self presentModalViewController:prodDetailViewController_ animated:YES];
But it just increases my work, because I will have to redo the top bar and bottom bar (which remains same in entire application) in all my views.
Is there a way that I just change the element of my main UIViewController to UITableView.
Thanking you in anticipation.
I'm going to assume you're able to use iOS 5 features here. What you can do is implement a container view controller - much like UINavigationController, but with your own view layout so you can keep logo, search bar, etc. all in place and only transition between views in a part of your view.
I created a new container view controller named ViewController. It has a UIView outlet containerView which is set up in the .xib file, along with a top bar, search bar, and bottom bar (corresponding to the other views you describe in your application). It also has properties tableViewController and detailViewController. In its viewDidLoad implementation, it adds a TableViewController instance as a child view controller. When the table view is tapped, the view controller adds a DetailViewController instance as a child view controller and transitions to it. Tapping a button on the detail view transitions back to the table view, and removes the detail view controller as a child.
Here's my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableViewController = [[TableViewController alloc] init];
self.tableViewController.delegate = self; // I implement a protocol TableViewControllerDelegate to know when row is tapped
// Fix for origin being 20 by default.
CGRect frame = self.tableViewController.view.frame;
frame.origin.y = 0.0f;
self.tableViewController.view.frame = frame;
[self addChildViewController:self.tableViewController];
[self.containerView addSubview:self.tableViewController.view];
[self.tableViewController didMoveToParentViewController:self];
}
I have a delegate method so that the container knows when a table row is tapped, and does the transition between the table view and the detail view. Its implementation follows:
- (void)tableViewController:(TableViewController *)tvc didSelectIndex:(NSInteger)index
{
self.detailViewController = [[DetailViewController alloc] init];
self.detailViewController.backButtonBlock = [self backButtonBlock]; // This block handles the transiton from detail back to table
CGRect detailStartingFrame = self.detailViewController.view.frame;
detailStartingFrame.origin.x = self.containerView.frame.size.width;
self.detailViewController.view.frame = detailStartingFrame;
[self addChildViewController:self.detailViewController];
[self transitionFromViewController:self.tableViewController
toViewController:self.detailViewController
duration:0.5
options:0
animations:^{
CGRect newTableFrame = self.tableViewController.view.frame;
newTableFrame.origin.x = (-1.0f * newTableFrame.size.width);
self.tableViewController.view.frame = newTableFrame;
[self.containerView addSubview:self.detailViewController.view];
CGRect newDetailFrame = self.detailViewController.view.frame;
newDetailFrame.origin.x = 0.0f;
self.detailViewController.view.frame = newDetailFrame;
} completion:^(BOOL finished) {
[self.detailViewController didMoveToParentViewController:self];
}];
}
As mentioned above, the detail view executes a block when tapping on a back button. I create this block in ViewController here:
- (GoBackButtonBlock)backButtonBlock
{
GoBackButtonBlock block = ^ {
[self.detailViewController willMoveToParentViewController:nil];
[self transitionFromViewController:self.detailViewController toViewController:self.tableViewController duration:0.5 options:0 animations:^{
CGRect newDetailFrame = self.detailViewController.view.frame;
newDetailFrame.origin.x = self.containerView.frame.size.width;
self.detailViewController.view.frame = newDetailFrame;
CGRect newTableFrame = self.tableViewController.view.frame;
newTableFrame.origin.x = 0.0f;
self.tableViewController.view.frame = newTableFrame;
} completion:^(BOOL finished) {
[self.detailViewController removeFromParentViewController];
[self.detailViewController.view removeFromSuperview];
}];
};
return [block copy];
}
That's about all there is to it. Be sure to read the "Implementing a Container View Controller" section of the UIViewController class reference for more details. Hope this helps!
Prepare all of your detail in a View, then add this view to the current screen. That's that.
in your "tableViewDidSelectRowAtIndex" method,On selection of row,just show the particular custom view (which contains details of selected row),and at that same time hide the table view,so u can have the view that u want. And make a back button on that custom view,and on that button action,hide your current view and show the tableview again.

iPhone -- structuring code to avoid a race condition

I have an iPhone app that uses a UINavigationController, some table views, and iAd. At the top level, my navigation controller shows the navigation bar. At lower levels, it does not.
The problem I am having is that sometimes the frame of my top level UITableView goes below the bottom of the screen. The reason it happens is this:
my viewWillAppear method looks like this:
-(void) viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated: animated]; // changing the last animated to NO does not help.
[super viewWillAppear:animated];
}
and my viewDidLoad method looks like this:
- (void)viewDidLoad {
[self.navigationController setNavigationBarHidden:NO animated: NO];
[super viewDidLoad];
[self createTableView];
ADBannerView *abv = [[ADBannerView alloc]initWithFrame: [self initialBannerViewFrame]];
abv.delegate=self;
[self.view addSubview:abv];
self.bannerView = abv;
[self moveBannerViewOffscreen];
[abv release];
}
Lastly, moveBannerViewOffscreen looks like this:
-(void) moveBannerViewOffscreen {
// moving it down and off
CGRect newBannerFrame = self.bannerView.frame;
CGFloat screenHeight = [[UIScreen mainScreen]bounds].size.height;
newBannerFrame.origin.y=screenHeight;
bannerView.frame = newBannerFrame;
CGRect newTableFrame = self.selectionTableView.frame;
newTableFrame.size.height = self.view.bounds.size.height;
self.selectionTableView.frame = newTableFrame;
}
When the view is loading, what happens is that even though I have called
[self.navigationController setNavigationBarHidden: NO animated: NO];
the the frame of my view is not immediately adjusted to account for the navigation bar. This is still true when moveBannerViewOffscreen executes. So the height of the table view is set to 480. When the navigation bar comes in, the result is that the bottom of the table view is below the screen, and the user can't select the last row.
I'm sure I could use an NSTimer to set up some kludge to fix this. But is there a clean way to organize my code so the problem doesn't come up in the first place?
Thanks
At first glance (without fully understanding your problem, I admit) I suspect that setting yourself as the navigation controller's delegate in order to take advantage of one of these methods would help with your timing:
navigationController:didShowViewController:animated:
navigationController:willShowViewController:animated:
perhaps not moving your banner until didShowViewController has been called.
(Apologies if I didn't follow your explanation.)

Transparent Background with a Modal UIViewController

I have a dilema, I want to present to the user a semi-transparent view.
I found out by experimenting that if I simply pushed the transparent view to the top of my NavigationController's stack, that it would not render the transparency level I wanted. So I decided to simply add the view as a subview of the current view at the top of the stack.
This solution works, the view below is still visible, and the View is 'semi-modal'. The problem is, if the parent view inherits from UITableViewController (as mine does), then the view I 'push' onto it, does not cover the navigation bar at the top.
I really don't want to get into a situation where I am forced to enable / disable controls on the navigation bar every time I push this view, so I was wondering, if anyone knew of any solutions that I could use so that the view I push onto the UITableViewController will actually 'push over' the navigation bar?
Funny, I was just doing the same thing yesterday. Unfortunately it seems to be impossible. Once the modal view controller is in place, the previous view becomes hidden.
See this previous question on the topic.
You can still use the view controller and NIB files you have set up - here's my sample code
- (void)showUpgrade {
[self.upgradeVC viewWillAppear:NO];
[self.view addSubview:self.upgradeVC.view];
[self.upgradeVC viewDidAppear:NO];
}
- (void)hideUpgrade {
[self.upgradeVC viewWillDisappear:NO];
[self.upgradeVC.view removeFromSuperview];
[self.upgradeVC viewDidDisappear:NO];
}
- (UpgradeViewController *)upgradeVC {
if (_upgradeVC == nil) {
_upgradeVC = [[UpgradeViewController alloc] initWithNibName:[NSString stringWithFormat:#"UpgradeView_%#", self.deviceType] bundle:nil];
_upgradeVC.delegate = self;
}
return _upgradeVC;
}
You will need to store a reference to the parent view controller in the modal view controller so that you can access the -hide method. I did this through a delegate.
It would also be easy to add some animation to -show and -hide if you want it to animate up from the bottom of the screen - I was just too lazy to do this.
iOS 8 added the UIModalPresentationOverFullScreen presentation style. Set this as the presented view controller’s modalPresentationStyle. For more advanced needs, look into creating a custom presentation controller.
There is now a way to achieve this using iOS7 custom transitions :
MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];
To create your custom transition, you need 2 things :
A TransitionDelegate object (implementing
<UIViewControllerTransitionDelegate>)
An "AnimatedTransitioning" object
(implementing <UIViewControllerAnimatedTransitioning>)
You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/
Try this:
ViewController *vc = [[ViewController alloc] init];
[vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
Have you tried looping over the Modal View Controller's subviews and setting the background color to clear for every view? This is a DFS recursive function.
- (void)setBackgroundToClearForView:(UIView *)view {
if ([view subviews]) {
for (UIView *subView in [view subviews]) {
[self setBackgroundToClearForView:subView];
}
}
if ([view respondsToSelector:#selector(setBackgroundColor:)]) {
[view performSelector:#selector(setBackgroundColor:)
withObject:[UIColor clearColor]];
}
}
To use it call:
[self setBackgroundToClearForView:self.view];
in viewDidLoad.
This will do the trick.. Try this one.
// for clear color or you can easily adjust the alpha here
YourVC *vc=[[YourVC alloc]initWithNibName:#"YourVC" bundle:nil] ;
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
So that the view will be full screen unlike UIModalPresentationFormSheet..