I'm not getting the rotation animation provided by the ViewController class. I thought I set it up correctly but something is wrong because its not happening and in the debugger I only hit the breakpoint at the function shouldAutorotateToInterfaceOrientation only once at startup but not when I rotate the device in the simulator. Does anyone know what I might be doing wrong?
GLViewController.m
#import "GLViewController.h"
#import "GLView.h"
#implementation GLViewController
- (void)loadView {
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
GLView *glView = [[[GLView alloc] initWithFrame:applicationFrame] autorelease];
self.view = glView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// We can run in landscape mode
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize m_window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.m_window = [[UIWindow alloc] initWithFrame: screenBounds];
//m_view = [[GLView alloc] initWithFrame: screenBounds];
m_viewController = [GLViewController alloc];
[m_window addSubview:m_viewController.view];
// Override point for customization after application launch.
[self.m_window addSubview: m_viewController.view];
[self.m_window makeKeyAndVisible];
}
You're not calling init on your GLViewController instance. This could cause all kinds of strange behaviour, including (perhaps) the rotation callbacks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.m_window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
m_viewController = [[GLViewController alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.m_window.rootViewController = m_viewController;
[self.m_window makeKeyAndVisible];
return YES;
}
I think you should try to create UINavigationController's object and push your view controller(GLViewController) on it in "didFinishLaunchingWithOptions". Add UINavigationController.view as a subview instead of view controller's view.
Related
I would like to know how can i change the view from a UIWindow class..for example, My app has a countdown timer view, when a user starts the timer, they can switch to other screen and the timer runs in the statusbar, what i want is when the user taps on the status bar (i have a custom button on the top of the status bar), it fires this method and change the view from current view to timer's view...
- (void)statusbarButtonTouched
{
NSLog(#"Button TOuched");
[self addSubview:??]
}
Create timerView in App delegate with defined property:
AppDelegate.h
#property (nonatomic, retain) UIView *timerView;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[FLViewController alloc] initWithNibName:#"FLViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
self.timerView = [[UIView alloc] initWithFrame:self.window.frame];
[self.timerView setBackgroundColor:[UIColor greenColor]];
[self.window addSubview:self.timerView];
[self.timerView setHidden:YES];
return YES;
}
Code to bring this view on front:
- (IBAction)shouTimerViewTouched:(id)sender {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.timerView setHidden:NO];
[delegate.window bringSubviewToFront:delegate.timerView];
}
That's it. You can pull for running demo from here https://github.com/rptwsthi/TrickTest.
Cheers.
as per docs
- (void)removeFromSuperview
Unlinks the receiver from its superview and its window, and removes it
from the responder chain.
Hi when using autolayout on iOS 6, my "rootView" of my rootViewController doesnt seem to resize properly if at all.
My rootViewController is loaded from a nib and i am adding it to the view-hierarchy like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
return YES;
}
But when i rotate the simulator the view of rootVC doesnt resize. Since you cant set any constraints to the "rootview" in a xib, I also tried this but without any effect:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
NSArray *vConst = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
NSArray *hConst = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
[self.window addConstraints:vConst];
[self.window addConstraints:hConst];
return YES;
}
When i log-out the frame-size of the rootvc's view, it's always the portrait-format (w:768, h:1024)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
NSLog(#"%f, %f", self.view.frame.size.width, self.view.frame.size.height);
}
could someone please tell me what i'm missing here? i've wasted hours with this problem and got nowhere^^
thx
It seems that a lot of things can go wrong with the autolayout feature iOS 6. Ambiguities in the constraints and failing to disable the translatesAutoresizingMaskIntoConstraints in your view controllers can cause much conflict. So ensure there is no ambiguities in the constraints by calling the autolayoutTrace method in lldb: po [[UIWindow keyWindow] _autolayoutTrace]
If there are ambiguities, you must resolve them.
I want to know that how can we setup a UIViewController in a UINavigationController without using UITableView to its Root View Controller. Any help regarding this? Thanks in advance.
You can do that as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
UIViewController *myViewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window.rootViewController = navigationController;
// ...
}
You can replace the class of myViewController and/or initialize it using a nib if you want.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:[[OptionViewController alloc]initWithNibName:#"OptionViewController" bundle:nil]];
self.window.rootViewController=nav;
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible];
return YES;
}
It's working for me:
set your tabviewcontroller on window, then it will work properly.
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UITabBarController *tabVC = [self.storyboard instantiateViewControllerWithIdentifier:#"MyMainView"];
[window setRootViewController:tabVC];
UINavigationController pushViewController:animated:
I have a subview added on app relaunch, on the applicationDidBecomeActive got called, but it's not shown, what could be the reason?
- (UIView *)mySubView {
if (_mySubView == nil) {
_mySubView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_mySubView.opaque = NO;
_mySubView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
_mySubView.exclusiveTouch = YES;
[_mySubView setUserInteractionEnabled:NO];
}
return _mySubView;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
[window addSubview:[self mySubView]];
}
What's more interesting is that - it'll shown if I leave the app launched, turn off the device, then turn the device back on. But if I close the app, then relaunch it, I won't be able to see the subview.
Please add the following line like this,
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
[window addSubview:[self mySubView]];
[window makeKeyAndVisible];
}
and also change the color [UIColor blueColor];
You will see the effect sure
Enjoy!
IF you're on your App delegate and you have a window property, as you should have, you could just simply do this:
[[self window] addSubview:[self mySubView]];
For setting the window property do this:
In your AppDelegate.h
#property (strong, nonatomic) UIWindow *window;
In your AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
.
.
.
//Create your main controller
.
.
.
[self.window setRootViewController:mainController];
[self.window makeKeyAndVisible];
I have a problem with switching from one viewcontroller to another.
Here is what I have :
In my "AppDelegate_iPad.m" :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
[window addSubview:loginView.view];
[self.window makeKeyAndVisible];
return YES;
}
That works. Now I have a login button on that screen and when pressed it calles this :
- (IBAction)doPressLoginButton
{
DebugLog(#"doPressLoginButton");
MenuViewController_iPad *menuView = [[MenuViewController_iPad alloc] initWithNibName:#"MenuViewController_iPad" bundle:nil];
[self.navigationController pushViewController:menuView animated:YES];
[menuView release];
}
The problem is that nothing happends. I assume I am missing the actual navigationconroller ?!
Hope that anyone can help.
Thank you
You should create a UINavigationController instance variable in your app delegate. Then, make sure you synthesize it and release it in your dealloc method.
Your implementation of application:didFinishLaunchingWithOptions: could look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// window is usually an IBOutlet
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginView];
[loginView release];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Now you'll be able to use self.navigationController.
I assume I am missing the actual navigationconroller ?!
you are right. :D
self.navigationController returns nil if you don't set up an NavigationController.
And any messages send to nil object will be ignored.
If you only need switch from one to another.
using
[self presentModalViewController:modalViewController animated:YES];
instead.