presentModalViewController ontop of subview? - objective-c

I am trying to recreate the iPhone's tabView, but with my own style, buttons, etc. I didn't want to have to totally redo my app, so I simply added a view to the bottom like this [window addSubview:theToolbar]; theToolbar.frame = CGRectMake(0, 425, 320, 44); in my appDelegate.
However, when trying to do this from a view inside a navigationController theToolbar is over it. Is there anyway to somehow present it to the front?
Here's my code to present the view:
AppSettingsController *appSettings = [[AppSettingsController alloc] initWithNibName:nil bundle:nil];
appSettings.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:appSettings animated:YES];
[appSettings release];
Thanks.

it's impossible show partial views of the viewcontroller. if your want to use the same toolbar, you should retain the toolbar to your appdelegate, and add the toolbar to each viewcontroller when it is in view.
or you should just use uiview's as viewcontrollers

Related

What exactly is navigationController.view.superview?

My project has existing code in several places where it uses navigationController.view.superview.frame and navigationController.view.superview.center to size and center modal views on top of current view. Sure I can use them as boiler plates but honestly I have no idea what navigationController.view.superview really refers to. Is it particular to UINavigationControllers? Any ideas/comments would be appreciated.
[Edit] Here's the code:
UIStoryboard *sb = self.storyboard;
MyViewController *vc = (MyViewController *)[sb instantiateViewControllerWithIdentifier:#"MyLoginViewController"];
vc.delegate= self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:navController animated:YES];
navController.view.superview.frame = CGRectMake(
navController.view.superview.frame.origin.x,
navController.view.superview.frame.origin.y,
400.0f,
400.0f
);
navController.view.superview.center = self.view.center;
If I don't use navigation controller for the new modal view, I wouldn't have to use "superview". Namely,
UIStoryboard *sb = self.storyboard;
MyViewController *vc = (MyViewController *)[sb instantiateViewControllerWithIdentifier:#"MyLoginViewController"];
vc.delegate= self;
[self.navigationController presentModalViewController:vc animated:YES];
vc.view.frame = CGRectMake(
vc.view.frame.origin.x,
vc.view.frame.origin.y,
400.0f,
400.0f
);
vc.view.center = self.view.center;
However, I wouldn't be able to view the presenting vc in the background...
navigationController - This is, well probably a UINavigationController. I say probably because you've not given any context so I can only assume that from the name.
navigationController.view - The navigation controller's view.
navigationController.view.superview - The navigation controller's view's superview. i.e. the view that the navigation controller's view sits in.
navigationController.view.superview.frame - The navigation controller's view's superview's frame. i.e. the frame (position and size) of the view the navigation controller's view sits in.
navigationController.view.superview.center - The navigation controller's view's superview's centre. i.e. the centre (position of the centre point) of the view the navigation controller's view sits in.
It's ugly to use these properties directly like that. I can't conceive of why you would want to ever go hunting into the depths of the view hierarchy like that to do anything. This to me is a bad smell. I suggest finding the right way of solving the problem.
In most cases, that probably refers to your instance of UIWindow. If you're trying to center things on screen, you can get the rect of the device's screen using [UIScreen mainScreen].applicationFrame.
As explained in the documentation for UIView, the method -superview returns the view's superview in the view hierarchy. That is, the superview is the view that contains the navigation controller's view. This is not specific to UINavigationController.

My UINavBar doesn't Respond to Touch, and Scrolls with tableview

I have a subclass of a UITableViewController, and I want to add a UINavBar to it. It is a very similar setup to the native contacts app, where you tap "add contact", and it presents a grouped tableview with a navbar at the top with a "cancel," and "done" option. The key is that I need it to present using a vertical transition (effectively with presentModalViewController:animated:yes), but I have tried using Interface Builder and adding it programmatically, and in both cases, the buttons do not respond, and the bar scrolls with the tableview, rather than staying at the top.
Thanks in advance,
HBhargava
It sounds like you're making the navigation bar a subview of the table view, that explains why the navigation bar scrolls with the table view.
Try this in the action method:
MyTableViewController *table = [MyTableViewController alloc] initWithStyle:UITableViewStyledGrouped];
UINavigationController *nav = [UINavigationController alloc] initWithRootViewController:table];
[self presentModalViewController:nav animated:YES];
Then in your table view controller's viewDidLoad:
UIBarButtonItem *doneButton = [UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
self.navigationItem.rightBarButtonItem = doneButton;

How can I set barButtonItems in a uiSplitViewController in iOS 5.1?

I am using the Master-Detail template from XCode 4.3.1 and want to access the toolbar on the detailView control.
[self navigationItem] setTitle:
sets the title, but I can't figure out how to add barButtonItems either through the xib or programmatically. In the past, with an earlier SplitView Template (when master view was called root view) I could access the rootView toolbar through the Split View Controller in MainWindow.xib and I had the detailView toolbar right there in the detailView.xib file.
What is the best way for me to set these items (I have 4) on the detailView toolbar?
Thanks in advance.
So I've got an answer - please let me know if you have a better one!
In the viewDidLoad method of my masterViewController I populated the toolbar on the detail view with
[_detailViewController.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: _detailViewController.firstButton, _detailViewController.secondButton, _detailViewController.thirdButton, nil]];
I had created these buttons with
_detailViewController.firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStylePlain target:self action:#selector(firstButtonAction:)];
I had declared each of these buttons in the detailViewController.h file so that I could reference them later, but probably didn't need to use those names - it just made the code longer here. I also had a uiButton that I could not add in the array of buttons, so I created a the button, put it inside a View and then set that as the titleView for the navigation item:
_detailViewController.biggerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 230, 35)];
[[_detailViewController biggerButton] addTarget:self action:#selector(biggerButtonTap:) forControlEvents:UIControlEventTouchUpInside];
UIView *biggerButtonView = [[UIView alloc] initWithFrame:CGRectMake(80, 3, 230, 35)];
[biggerButtonView addSubview:_detailViewController.biggerButton];
_detailViewController.navigationItem.titleView = biggerButtonView;

Issues with changing UINavigationBar background when presenting modal view controller

I am using this method to change the backgrounds of my uiviewcontrollers. It generally works when I push a view controller.
However, if the viewcontroller is presented using
[self presentModalViewController:customViewController animated:YES];
then, this code doesnt work. Can anyone kindly suggest whats wrong ?
Code used:
To have an image in the navigation bar, you have to draw it yourself, which actually isn't that hard. Save this as UINavigationBar+CustomBackground.m (it adds a custom category to UINavigationBar):
#implementation UINavigationBar (CustomBackground)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"NavMain.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
If you are running on iOS 5, then drawRect: is no longer called
You will need to either use the UIAppearance or subclass UINavigationController and use that to change to you image.
A tutorial for UIAppearance can be found here
(drawRect: will still work on versions below iOS 5)
Try this ,
UIColor *image = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"imagename.png"]];
replace image name.
not sure if this is it but have you tried
[self presentViewController:customViewController animated:YES completion:NULL]
And setting whatever modalViewStyle you deem appropriate for customViewController? According to the iOS documentation, presentModalViewController is deprecated, so you may have better luck using the above message call (especially since you only seem to be having this issue with modal view controllers)
You have to create a navigation controller, set the root controller and present it.
e.g.
UIViewController *vc = [[UIViewController alloc] init];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:cntrol animated:YES];
The method you is to set the navigation bar image in navigation controller.
But The new view controller you present is not using navigation method. Instead, you use Modal view Controller.
I have two solution for you:
--- 1 -----
still use Modal View Controller, just add that image on the top of new view controller.
[self.view addSubview:theImage];
---- 2 -----
use
[self.navigationController pushViewController:customViewController animated:YES];
instead of
[self presentModalViewController:customViewController animated:YES];
In this case, you are using navigation.
I will suggest the second one.

iOS: Trying to add Navigation Bar to Modal UITableViewController

I'm following the CoreDataRecipes app for modaly showing the add screen when I want to add a new item. However I cannot get the bar to display at the top so I can press 'Done' or 'Cancel'.
In the xib calling the modal controller I have the + button linked to modally sliding up the controller via IB.
I have the below in my modal controller
self.navigationItem.title = #"Add";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
self.navigationController.navigationBarHidden = NO;
In my viewDidLoad
The modal controller displays fine except there is no bar so I cannot leave that screen.
You need to add it before the popover is actually presented.
Where you create the modal popover, you need to create it inside a UINavigationController first.
So, do the following.
PopoverView *foo = [[PopoverView alloc] initWithNibName:#"PopoverView" bundle:nil];
// Here you pass through properties if you need too.
// ...
UINavigationController *navC = [[UINavigationController alloc] initWithRootView:foo];
[foo release];
[self.navigationController presentModalViewController:navC animated:YES];
That will give the modal view the navigation bar which you're trying to edit.
Alternatively, you could maintain your storyboard segue. In Xcode, select the view controller you are trying to transition to and embed it in a navigation controller.
Now in the viewDidLoad of that view controller, add:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
and lastly the callback:
- (void)cancel {
[self dismissModalViewControllerAnimated:YES];
}
Or if you just need the look of the bar, not exactly its functionality, you could drag the Navigation Bar (UINavigationBar) or Toolbar (UIToolbar) controll from the Media Library panel onto your view and go from there.
I had a similar predicament whereby I was loading a UITableViewController in a containerView. The containerView was inside a UIViewController which was being presented in a modal fashion.
I, like you, needed the navigation bar to have a title and a Done/Cancel button.
After overflowing the stack, I finally did this -
Dragged a UIView as the first item in the Table View in the IB. This automatically took a height of 44 pts and snapped to the top. It also shifted my first section downwards.
I dragged a UIButton (Done button) inside this view. Created an IBOutlet to it and called
[self dismissViewControllerAnimated:YES completion:nil];
Disclaimer:
1) This fake nav-bar will scroll along with the tableview.
2) This might not be a solution for what Bot has asked, but it's an option for others who might be looking for something similar.