[[[UIApplication sharedApplication] keyWindow] addSubview:myView];
The above code is working fine in iPhone 5.1 simulator, but the view is not appearing in iPhone 6.0 simulator. What could be the problem?
You have to send makeKeyAndVisable to what ever window you want to add the subview to. Like so:
[[self window] makeKeyAndVisible];
[[[UIApplication sharedApplication] keyWindow] addSubview:view];
The key window is the window which will receive user interaction. You can check the apple docs here for makeKeyAndVisable.
Related
I'm checking out the new Xcode 6 Storyboards for os x application
I want to do the same like when doing push segue in iOS side.
But the new Xcode storyboards for os x application doesn't have the push segue.
So you can see there is no push. How to achive the same functionality?
I haven't tested it, but I've used a similar approach on my iPad app. It needs some tailoring for Cocoa:
-(void) perform {
UIView *source = ((UIViewController *)self.sourceViewController).view;
UIView *destination = ((UIViewController *)self.destinationViewController).view;
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
destination.center = CGPointMake(source.center.x + source.frame.size.width, destination.center.y);
[window insertSubview:destination aboveSubview:source];
[UIView animateWithDuration:0.4
animations:^{
destination.center = CGPointMake(source.center.x, destination.center.y);
source.center = CGPointMake(0- source.center.x, destination.center.y);}
completion:^(BOOL finished){
[source removeFromSuperview];
window.rootViewController = self.destinationViewController;
}];
}
Let me know if it works.
Depending on what you are trying to do, a NSTabViewController might be appropriate. It lazy-loads view controllers and can transition using a slide animation.
I have an app that presents a UIWebview modally using presentViewController.
Now the root view supports orientation changes and rotates accordingly. I listen to the event UIApplicationWillChangeStatusBarOrientationNotification.
Now I present the modal window using "
presentViewController:navController
animated:YES
completion:nil];
I dismiss using
dismissModalViewControllerAnimated:YES];
now to the issue :
in iOS 6, this works fine and the modal window shows up in whatever orientation its parent was in.
in iOS 5 , the presentViewController or dismissModalViewControllerAnimated forcibly throws
UIApplicationWillChangeStatusBarOrientationNotification!!
Any ideas why?
Try using this orientation notification instead:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
So my app has been around for 5 days now and iPhone 3GS users are reporting that their notifications doesn't cancel when the button is switched.
The button executes the following code:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
However, this code works on iPhone 4,iPhone 4S,iPhone 5 and iPad.
Is this a known bug of the iPhone 3GS? Or does the 3GS require different code to achieve the same thing?
I'm running into this problem on iOS 8.1 on an iPhone 6+. It turns out that cancelAllLocalNotifications is not a synchronous call, so there is no guarantee that the cancelation has been completed when it returns. I've had to implement the following as a workaround
- (void) clearNotifications
{
NSLog(#"Clearing notifications");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
long count;
while ((count = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]) > 0) {
NSLog(#"Remaining notificaitons to cancel: %lu",(unsigned long)count);
[NSThread sleepForTimeInterval:.01f];
}
NSLog(#"Notifications cleared");
}
Don't know why but I had to cancel all notification by notifcation ID instead of CancelAll on iPhone 3GS
My iOS application supports all orientations except PortraitUpsideDown.
But in the application I have an view with preferences which I want it to only be shown in Portrait orientation. So whenever this view is shown, it is rotated if needed, to be in portrait mode. That means that user will rotate device in portrait mode also, to setup preferences, and then after closing this view interface should now have portrait orientation.
The problem is, that after preferences view is hidden interface stays in landscape orientation, since I block autorotation after this view is shown.
So after the view is hidden I want to manually update the interface to current device orientation. How can I do it?
self.view.hidden=NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.view.alpha=1.0;
[UIView commitAnimations];
This code is called from the OptionsViewController after a LongPressGesture on its superview.
I created a UIViewController extension to force update of orientation of a controller, based on the solution presented by Marek R. Since the new versions of iOS, his solution does not work anymore. I post here my solution to force orientation update (in order to take into account supported orientation methods of controller) without having side visual effects. Hope it helps.
UIViewController *vc = [[UIViewController alloc]init];
[[vc view] setBackgroundColor:[UIColor clearColor]];
[vc setModalPresentationStyle:(UIModalPresentationOverFullScreen)];
[self presentViewController:vc animated:NO completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[vc dismissViewControllerAnimated:YES completion:completion];
});
}];
All you have to do is add the following to the view controller you're using for your preferences.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Calling this workaround works for me:
-(void)forceOrientationUpdate {
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];
}
I've been researching this all day but I haven't found anything about the rootViewController in relation to this error message. I know what the problem is but have no idea how to fix it. My problem is that my window.rootViewController is not connected or shows null and I can't figure out what to do. I've tried everything I could think of in code and in IB, but bad things happen whenever I change something. This is the message I get: "Application tried to push a nil view controller on target UINavigationController"
I can see the window.rootViewController from an NSLog statement:
"window.rootViewController : (null)"
of course, everything was working perfectly before upgrading my Xcode to 4.2 and ios5. :)
btw - the view loads but I cannot work any of the buttons, they do not light up at all. And my navigation works fine too.
here is my appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
NSLog(#"Viewcontroller : %#", self.viewController);
// Set the view controller as the window's root view controller and display.
//self.window.rootViewController = self.viewController;
//do it this way, previous version not supported in ios5 - may need to check version for compatibility
[self.window addSubview:self.viewController.view];
//set up navigation controller
NSLog(#"window.rootViewController : %#", self.window.rootViewController);
navigationController = [[UINavigationController alloc]
initWithRootViewController:self.window.rootViewController];
navigationController.navigationBarHidden = YES;
NSLog(#"navigationController : %#", navigationController);
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
//force this view to be landscape
[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
[self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
[self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];
[UIView commitAnimations];
return YES;
}
Thank you.
According to the docs:
Discussion
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.
Double check your nib file to make sure it is connect.
Although the "Application tried to push a nil view controller on target UINavigationController" sounds like you maybe losing your VC reference. How is the property set for this? Is it retained?
Here are the docs.
http://developer.apple.com/library/ios/documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/doc/uid/TP40006817-CH3-SW33