I'm presenting a UINavigationController modally, from within an iOS app extension:
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationFormSheet;
nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:nav animated:YES completion:nil];
When the navigation controller appears, its root view controller's UIBarButtonItems jump position:
I'm creating and adding the buttons in viewDidLoad. They are just standard bar button items:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
I'm not overriding viewDidAppear (which appears to be the point where the buttons jump).
Presenting this same navigation controller/root view controller from within my app, instead of the app extension, doesn't give me this same problem. Any ideas?
I'm hoping someone else finds a better way to do this, but my current solution is to create stand alone buttons and then use [[UIBarButtonItem alloc] initWithCustomView:] with the button. Not ideal, and the indention is still there, but the weird jump doesn't happen anymore.
If the indention bothers you, you could also set negative fixed spaces on the outsides of the buttons to shift them closer to the edges.
Here's an example including the spacer:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
<other button config here, set targets, whatever>
[button sizeToFit];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:-8.0f];
self.navigationItem.leftBarButtonItems = #[fixedSpace, leftBarButton];
This only happens to me on my share extension, and I don't have any problems related to presenting from a detached controller or anything like that.
As #Stonz2 mentioned, this appears to be a characteristic of presenting a modal from a detached view controller. I was having the same problem and remedied it by re-organizing my app so that it wasn't presenting from a detached controller.
You'll know if you're presenting from a detached controller if you get the following error message:
Presenting view controllers on detached view controllers is discouraged
I was presenting from a detached viewController. I never got the warning. But I tried embedding that detached viewController in a NavigationController, and the problem went away.
Related
I have an iOS 6 app that I am updating to work with iOS 7 and to make it use storyboards. I am trying to use multiple storyboards so that I can break my project down into modules for each screen in the app. So far this has worked out fine but now I need to provide a way to navigate between the various storyboards while still making the work like it did in iOS 6 (but with updated artwork).
I don't use UINavigationController in my existing iOS 6 app and I would prefer not to use it as up to now I have been able to navigate back and forth between XIB's using code on UIButton tap gestures. The UINavigationController doesn't make it easy to customise how the navigation buttons look from what I have learned so far about it.
I found this very clean way of moving between view controllers that are on different storyboards https://github.com/rob-brown/RBStoryboardLink by passing the name of the storyboard in as an attribute.
But it only seems to work when UINavigationController is used. I get an error "Push segues can only be used when the source controller is managed by an instance of UINavigationController" without UINavigationController.
Is there a way to navigate between storyboards by only using the above RBStoryboardlink but without the need for UINavigationController?
Push segues can only be used when the source controller is managed by an instance of UINavigationController
This means you are trying to push a view controller to the source while source don't have any navigation controller stack.In that case, you should try to add the instantiated view controller's view as subview to the source view controller's view.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [mainStoryboard instantiateInitialViewController];
[self.view addSubview:viewController.view];
or you modally present, that purely depends on your requirement.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [mainStoryboard instantiateInitialViewController];
tabBarViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:tabBarViewController animated:NO completion:NULL];
How do I create a home button in the top right hand side of my UINavigationControl. Please note that I have instantiated my UINavigationControl on my second UIviewController named: ViewControllerSecond. I want to be able to have the tradition "Home" icon, and when pressed to resign back to my first UIviewControler within the appDelegate named: ViewController.
I've got this far:
#property (nonatomic, weak) UIBarButtonItem *homeProperty;
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(home)];
[self.navigationItem setLeftBarButtonItem:homeButton animated:NO];
But I cant link it with my first ViewController or replace the text with a home button image.
Many thanks
I also need the answer to this question as i couldn't find an answer to this question anywhere else.I would hav voteup'd this question if i had 15 repu :(
btw i want the selected items in my tableview to be posted on a new view controller when the "done" button on my top-right navigational bar gets clicked.I have also reached the same distance as the asker of this question.
Forgive me for posting my words in the answer section to this question as i can't post in comments to that question due to that 50 repu :(
EDIT:I think i found an answer,try this plz
Since you hav action:#selector(home) so your method name will be:
- (IBAction)home:(id)sender {
//statements to execute
}
You don't have to link this method with anything as you have already declared this method as selector while declaring that "home" button.Neglect the open linkage in front of your method.
http://prntscr.com/1tc0q9
I am trying to create an app that uses cocos2d for the homescreen, and a UINavigationController for some tables and information. I use this code to try to push a UINavigationController (Settings) into view
UINavigationController *controller = [[UINavigationController alloc] init];
Settings *newTableViewController = [[Settings alloc] init];
[controller pushViewController:newTableViewController animated:YES];
This code is executed in a CCLayer, and Settings is a UINavigationController.
I get this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
Does this mean that I can't push a NaviagationController into a CCLayer?
Please Help.
You cannot "push view controller to the CCLayer". All that you are doing using cocos2d takes place inside one OpenGL view.
If you really want to do something like this, you can change creation of the application's root view controller. Make your application view controller a subclass of UINavigationController instead of UIViewController, then just get rootViewController from your app delegate and push/pop any other view controller to it whenever you want. Anyway, I don't think, that it is a good idea.
About your exception I assume that your Settings class is a subclass of UINavigationController. All the controllers that you want to push to the navigation controller must be derived from UIViewController, not UINavigationController.
I have been struggling with switching between views using the UINavigationalController. I have used this system many times without issue but in my new app it isn't working properly.
Here is the issue:
When i am pushing a new view controller i use the following code:
NewViewController *newVC = [[NewViewController alloc] initWithNib:#"NewView" bundle:nil];
[self.navigationController pushViewController:newVC animated:YES];
[newVC release];
The code I am using to return to the previous view inside of the newVC is:
[self.navigationController popViewControllerAnimated:YES];
I was reading that this could potentially be releasing the self.navigationController itself so I implemented this code:
UINavigationController *nc = [self navigationController];
[nc popViewControllerAnimated:YES];
What results is a smooth transition to the newVC with no white flash, but when returning to the original page the screen flashes white as if it is releasing the newVC before transitioning back to the original page. HOWEVER! When debugging I placed breakpoints on viewWillAppear of the original page and on the dealloc of the newVC and the viewWillAppear + transition with white flash all complete BEFORE the dealloc of the newVC is called.
If anyone could please help shine some light on this I would greatly appreciate it.
Thanks!
~Arash
This is an old post, but for those who may run into this issue in the future, I have solved it by setting the clipsToBounds property of the view of the ViewController to "TRUE"
-(void)viewDidLoad {
[super viewDidLoad];
self.view.clipsToBounds = YES;
}
Try changing the background colors of the various views on the navigation stack to different recognizable colors (including the main window). One of them might be showing for some reason, and if each one has a different color you can determine which one is the culprit pretty easily.
FWIW, this same issue happened for me in a Swift app. The root cause appeared to be that I was doing this:
self.navigationItem.rightBarButtonItem = nil
...to dynamically hide the button, where the UIBarButtonItem had an outlet in the current UIViewController.
I did not actually need an IBOutlet for that button, so I removed the outlet, and it worked.
I currently have a tab bar controller set up with a navigationcontroller on one of the tabs, then I have a UITableView nib set up for that Navigationcontrollers view. All of this has been set up through IB and I want to keep it that way. Kind of like this tutorial http://twilloapp.blogspot.com/2009/05/how-to-embed-navigation-controller.html
now the view loads perfectly when ViewDidLoad is called. But when I then load further views via code IE
MyApp_AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UINavigationController *nav = (UINavigationController *)delegate.controller.selectedViewController;
newViewController = [[newViewController alloc] initWithNibName:#"newView" bundle:nil];
[nav pushViewController:newViewController animated:YES];
//At this point the view works! and loads
If I try to go back with the navigation toolbar it goes back to my previous view fine
Now I need to refresh the tableview when I go back but calling viewDidAppear does not work.
I tried adding UINavigationDelegate to the same ViewController Class as the tableview and then calling - (void)navigationController:(UINavigationController *)navigationController didShowViewController:
But that did not work. I also tried adding the same delegate as the tab bar controller and adding the same navigationController didShowViewController: there but that also failed.
How do I get this table to refresh every time the view loads?
You should not have to call viewDidAppear from your code. Cocoa Touch should do that for you.
Call the table view's reloadData method to get it to refresh its contents.
Found I was missing the Delegate declaration in the Interface file. doh! also I tried that in lots of places it only ended up working when I added it to the NavigationControllers first view (my table view)