I have the following code in my main ViewController viewDidLoad function
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
nav = [[NavWithAutoRotateViewController alloc] initWithRootViewController:self];
[window addSubview:[nav view]];
[window makeKeyAndVisible];
[[self navigationController] setNavigationBarHidden:YES];
My ipad app is currently set to only work in landscape mode and I'm using this new window to show a quicklook document and allowing the nav bar to provide a back button and save options for the document. Main problem is that the new UIWindow orientation doesn't match my main applications UIWindow.
I have a custom UINavigationController above called NavWithAutoRotateController and here is the code for that controller.
-(id)init
{
if(self)
{
// _supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
// _orientation = UIInterfaceOrientationLandscapeLeft;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
// Tell the system which initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskLandscape;
}
I think I have a solution. The problem seems to be in the assignment of UIViewController to the rootViewController in your extra UIWindow.
If you just assume that you can use the same view controller that your primary UIWindow is using, and then add things as subviews of the new UIWindow, there are orientation issues.
To solve this, I did the following:
UIWindow *newWindow = [[UIWindow alloc] initWithFrame: [UIApplication sharedApplication].keyWindow.frame];
newWindow.windowLevel = UIWindowLevelStatusBar+1;
// You can use a view controller instantiated from a xib or storyboard here if you want.
// Just don't use the view controller already set as a UIWindow's rootViewController.
UIViewController *newViewController = [[UIViewController alloc] init];
newWindow.rootViewController = newViewController;
[newWindow makeKeyAndVisible];
// Add something to the new UIWindow. Can use the new UIViewController's view:
[newViewController.view addSubview: myContentView];
// Or could add it as subview of UIWindow: - either works.
[newWindow addSubview: myContentView];
Doing it this way seems to have solved all the weird issues around rotation.
Hope this helps.
I was having the same problem. Orientation changes were being handled properly using the viewWillTransition method. But my problem was that in some edge case conditions, namely if my device was sitting at an odd angle, the custom UIWindow would initialize in portrait orientation, even though the rootViewController was in landscape. And viewWillTransition isn't called because the device isn't rotated on initial load. I found a really simple solution to my problem that worked in any situation I tested.
First initialize your custom UIWindow without a frame. Then set your frame. And voila.. its orientation is what you'd expect.
Swift
let customWindow = UIWindow()
customWindow.frame = CGRect(x: x, y: y, width: w, height: h)
customWindow.rootViewController = self //<your_viewController>
customWindow.windowLevel = .statusBar // or whatever level you need
customWindow.makeKeyAndVisible()
ObjC
UIWindow customWindow = [[UIWindow alloc] init];
customWindow.frame = CGRectMake(x, y, w, h);
customWindow.rootViewController = self;
customWindow.windowLevel = UIWindowLevelStatusBar;
[customWindow makeKeyAndVisible];
Register for status bar frame change notification which is only called (afaik) when orientation changes and then define your new window's orientation..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillChangeStatusBarFrameNotification:)
name:UIApplicationWillChangeStatusBarFrameNotification
object:nil];
Related
I'm creating a simple application with uitableview. I want to create everything in code. I used following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FBVCalendarViewController *calendarViewController = [[FBVCalendarViewController alloc] init];
self.window.rootViewController = calendarViewController;
[self.window makeKeyAndVisible];
return YES;
}
...
- (void)loadView
{
UITableView *calendarItems = [[UITableView alloc] init];
self.view = calendarItems;
}
it works, but application fills the entire phone screen intersecting with standard phone title bar.
What is the right way to adjust view height?
Since UITableView inherits from UIScrollView, you should take care of the changes appeared with IOS 7.
A solution to your problem is:
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)]) {
[self setNeedsStatusBarAppearanceUpdate];
self.automaticallyAdjustsScrollViewInsets = NO;
}
(this will keep the table view below the status bar).
Hope that helps. But you should probably have a look at changes introduced with IOS 7.
So I solved my problem with the following code in loadView:
- (void)loadView
{
UITableView *calendarItems = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
UIView *rootView = [[UIView alloc] init];
[rootView addSubview:calendarItems];
self.view = rootView;
}
I used empty UIView as a parent for tableView and changed constructor to explicitly specify UITableView frame. I think that better approach would be to use autolayout (currently it just does not work as expected when I rotate device) and position table view to the full screen or implement device rotation callback and update frame there.
My app has a root viewcontroller, which at the start of the app displays
login viewController view if the user is not logged in
main viewController view if the user is logged in
AppDelegate code:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[RootViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
Here's the code used in RootViewController:
#implementation RootViewController
- (void) loadView
{
[super loadView];
// here mainViewController and loginNavigationController are initialized
}
...
- (UIView*) view
{
[super view]; // this invokes loadView
return self.isLoggedIn ? self.mainViewController.view :
self.loginNavigationController.view;
}
....
- (void) userDidLogin
{
[self.loginNavigationController presentViewController: self.mainViewController
animated: YES
completion: nil];
}
#end
If the user is not logged in and presses login button the main viewController is presented.
The problem is that after main viewController is presented, I'm not able to interact with any of the UI elements. For example, I have a tableView as a main viewController's subview and when I try to scroll it I get the following warning in debug panel:
<UITableView: 0x202a4000; frame = (0 0; 310 548); clipsToBounds = YES;
gestureRecognizers = <NSArray: 0x1fd9f570>; layer = <CALayer: 0x1fdccff0>;
contentOffset: {0, 0}>'s window
is not equal to <RootViewController: 0x1fd9f7d0>'s view's window!
Ok, so after looking at the updated code I see that you have a rootViewController and are dynamically giving the view you think should be presented. The thing is, the rootViewController is in charge of the root view while your other two view controllers manage their own views. You should not be passing a different view controller's view off.
So in the end it looks like you want to conditionally set your rootviewcontroller. So lets look at the app delegate. I think you should make your app delegate do something like this. Have it figure out at runtime which viewcontroller to present. Then make that the rootviewcontroller for the app.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController * resolvedRootViewController = [self someMethodThatCorrectlyGivesRootViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = resolvedRootViewController;
[self.window makeKeyAndVisible];
return YES;
}
I have an app that uses a transition file to flip from page to page. I am using ARC and works just fine on 5.1, but crashes all the time on 4.3 simulator. Looking at the thread and the extended detail from instruments, it points to 2 lines of code (shown below) with the error: EXC_BREAKPOINT (code=EXC_I386_BPT). Looks like the UIViewController is being deallocated. Not sure how to fix this. Thanks in advance for any help you can offer!
#synthesize containerView = _containerView;
#synthesize viewController = _viewController; //ERROR LINE#1
- (id)initWithViewController:(UIViewController *)viewController
{
if (self = [super init])
{
_viewController = viewController;
}
return self;
}
- (void)loadView
{
self.wantsFullScreenLayout = YES;
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;
_containerView = [[UIView alloc] initWithFrame:view.bounds];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_containerView];
[_containerView addSubview:self.viewController.view];
}
- (void)transitionToViewController:(UIViewController *)aViewController
withOptions:(UIViewAnimationOptions)options
{
aViewController.view.frame = self.containerView.bounds;
[UIView transitionWithView:self.containerView
duration:0.65f
options:options
animations:^{ [self.viewController.view removeFromSuperview];
[self.containerView addSubview:aViewController.view];}
completion:^(BOOL finished)
//ERROR LINE#2 { self.viewController = aViewController;}];
}
You should not be transitioning views in and out from underneath their respective view controllers. You might get it to work, but it's fragile, at best.
If you want to future-proof your code, you should be transitioning between view controllers and let them handle their own views, but wrap that in the desired animation if you don't like the default animation. So, you should either:
Just do simple presentViewController or pushViewController to go to the next controller and dismissViewController or popViewController to return; or
In iOS 5, you can do your own container view controller (see session 102 in WWDC 2011 or see the discussion of view controller containment in the UIViewController reference) and then you can transitionFromViewController.
If we knew more about your app flow, why you're doing what you're doing, we can probably advise you further.
I created a brand new project and created a new view controller with a button in the view.
I am adding the view in application:didFinishLaunchingWithOptions
- (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];
BOOL introDisplayed = [[NSUserDefaults standardUserDefaults] boolForKey:kIntroScreenSeenByUser];
if(introDisplayed)
{
}
else
{
IntroView *introView = [[IntroView alloc] initWithNibName:#"IntroView" bundle:nil];
[self.window addSubview:introView.view];
}
[self.window makeKeyAndVisible];
return YES;
}
.h file
#interface IntroView : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *clickMe;
- (IBAction)clicked:(id)sender;
#end
.m file
#import "IntroView.h"
#interface IntroView ()
#end
#implementation IntroView
#synthesize clickMe;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setClickMe:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)clicked:(id)sender {
NSLog(#"clicked");
}
#end
Clicking on the button results in a EXC_BAD_ACCESS(code=2 error. Any ideas? I am using ARC.
Thanks
UPDATE
Created a public property on the application delegate called "introViewController" and changed the application:didFinishLaunchingWithOptions
#synthesize introViewController;
- (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];
BOOL introDisplayed = [[NSUserDefaults standardUserDefaults] boolForKey:kIntroScreenSeenByUser];
introViewController = [[IntroView alloc] initWithNibName:#"IntroView" bundle:nil];
if(introDisplayed)
{
}
else
{
[self.window addSubview:introViewController.view];
}
[self.window makeKeyAndVisible];
return YES;
}
This solved the error.
You're creating your IntroView controller, and adding it's view as a subview, but the controller itself is released. I don't think that adding the view controller's view (and then letting ARC discard the controller itself) is an acceptable way to create a view.
Perhaps you could make the IntroView view controller a property of the app delegate class and therefore it won't be released by ARC.
Personally, I don't monkey around with the app delegate's creation of the views and controllers, but rather I let my target settings and my NIBs dictate that. I presume you're doing this because you want to have some intro screen. If I wanted an screen, I'd have my main view controller go ahead and present whatever intro I want. That way when the intro is dismissed (or popped off, depending upon whether you pushed or presented modally), my main view controller is still at the top (and useful methods like popToRootViewController work perfectly).
Hello fellow programmers,
First, sorry for the long post. My question is rather simple, but I want to make sure you know what I'm doing and I really don't want to change the basic idea of my approach.
(the following is all done programmatically, no storyboards, no nibs, no navigationcontroller)
I have a RootViewController without an own view. All he does is instantiate other ViewControllers, manage their transitions and push (add) their views into the main window. To position these views correctly, I want to get bounds/frame for one of the RootViewCOntrollers SubControllers. This is how I create the RootViewController from the appDelegate (I started with a empty project)
- (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 grayColor];
self.window.rootViewController = [[UCRootViewController alloc]init];
[self.window makeKeyAndVisible];
return YES;
}
After his initialization, the rootviewController creates a MapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"RootviewController initialized");
self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self];
self.view = self.mapviewController.view;
[self.mapviewController.view setOpaque:YES];
[self.view bringSubviewToFront:self.mapviewController.view];
[self presentModalViewController:self.mapviewController animated:YES];
self.currentlyActiveController = self.mapviewController;
}
The MapViewController creates a navigationBar and a MKMapView. Right now I set the frames hardcoded, because I'm not able to get the bounds/frame of the window in the viewDidLoad() of the MapViewController When I try to get any infos about bounds/frame, I get 0 returned.
- (void)viewDidLoad
{
NSLog(#"MapviewController initialized");
[super viewDidLoad];
self.isMapViewPushedAside = NO;
// Custom initizialation for navigationBar
[self setupNavigationBar];
// Custom initialization for mapview
[self setUpMapview];
[self trackUserLocation];
// Custom initialization for popupActionsButton
[self setUpPopupButtons];
// Custom tests
[self test];
[self focusLocationOnMap:self.locationManager.location.coordinate];
[self.locationManager stopUpdatingLocation];
}
I've implemented two delegate methods that return frame/bounds (same) for the window. The problem is, I must get those values at the start, not after everything has been initialized. when I call the delegate methods from a button after everything is up, they work as expected.
CGRect frame = [self.mapDelegate frameData];
NSLog(#"width by frame: %f", frame.size.width);
NSLog(#"height by frame: %f", frame.size.height);
CGRect bounds = [self.mapDelegate boundsData];
NSLog(#"width by bounds: %f", bounds.size.width);
NSLog(#"height by bounds: %f", bounds.size.height);
How do I obtain the frame/bounds at the start, that is, before calling my custom "setup" methods..?!
I have a RootViewController without an own view.
You can't have a UIViewController without a view. If you have the app will crash. When you initialize a UIViewController it automatically creates a UIView for you.
- (void)viewDidLoad
{
[super viewDidLoad];
..
self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self];
self.view = self.mapviewController.view;
..
}
From what can I see here, you're actually setting the RootviewController's view to be the map view. This should be done by overriding the -(void)loadView method of your controller and there you need to set the view:
-(void)loadView
{
self.mapviewController = [[UCMapviewController alloc] initWithDelegate:self]; //if you're not using ARC this should be autoreleased;
self.view = self.mapviewController.view;
}
When viewDidLoad method is called there is no geometry set in any of the views of your controller. They are only initialized (implicitly or explicitly by -(void)loadView) and viewDidLoad is called just right after that. Geometry is setup at earliest in viewWillAppear: method and the consecutive viewDidAppear: method, so viewWillAppear: is the earliest point you can have your actual frame/bounds of your views and in viewWillAppear: method you should only execute some lightweight operations (like setting geometry, starting timers, subscribe observers, etc..).
You said you don't want to change your approach, but you need to design according to these rules.