I am trying create app without Story Book and xibs. It must work landscape mode only.
But I got trouble:
http://i.stack.imgur.com/FPzkk.png
Red - it's view of my controller. It's looks like it in wrong position and not rotated.
For create I use this code in AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
CGRect screenRect = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:screenRect];
EmulationViewController* cntr = [EmulationViewController sharedController];
[self.window addSubview:cntr.view];
[self.window makeKeyAndVisible];
return YES;
}
And
- (void)loadView
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
EmuWindow* view = [[EmuWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor =[UIColor redColor];
self.view = view;
}
It's tested on simulator with iOS 8. What I do wrong?
You need to make the view controller the root view controller, instead of this:
[self.window addSubview:cntr.view];
... do this:
self.window.rootViewController = cntr;
If you don't do this it won't get automatically rotated...
Then if you want it to always be in landscape, you can add this to the view controller code:
-(NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Related
I am using the following code in my app delegate to programmatically create the application's window and root controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
self.window.rootViewController.view = [[UIView alloc] init];
self.window.rootViewController.view.backgroundColor = [UIColor redColor];
return TRUE;
}
The code works fine, however the view associated to the root view controller overlaps the status bar, which I would like to avoid (see picture below).
How should I proceed? In particular I want to have a lightgray background for the status bar (similar to the apps created with a storyboard) and also handle orientation change correctly.
Thanks
Well, you can do something as following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0,
[UIApplication sharedApplication].statusBarFrame.size.height,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
redView.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:redView];
[self.window makeKeyAndVisible];
return TRUE;
}
should look like this:
I updated Xcode today.
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc = [[ViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.vc];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect f = self.view.frame;
f =[[UIScreen mainScreen] bounds];
self.view.frame = f;
self.view.backgroundColor = [UIColor redColor];
}
The frame is showing as 320x480. I can clearly see more space around the view, but it's black. Why is the view not filling out? There is no XIB.
In my appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect newFrame = CGRectMake(100, 200, 50, 150);
OtherView *newView = [[OtherView alloc] initWithFrame:newFrame];
//UIView *newView = [[UIView alloc] initWithFrame:newFrame];
newView.backgroundColor = [UIColor redColor];
[self.window addSubview:newView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
OtherView is simply a blank class that inherits from UIView.
When I use the commented out code instead of Otherview, my CGRect is in the correct position, but when I use the code above as is, my CGRect is off-screen on the top left.
Try clean project CMD+SHIFT+K, you have not import OtherView.h or wrong inherits UIView
I am writing an iPhone app using custom UIWindow.(xCode6.1, iOS8.1)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
CGRect bounds = [[UIScreen mainScreen] bounds];
_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bounds.size.height, bounds.size.width)];
[_window setBackgroundColor:[UIColor greenColor]];
[_window makeKeyAndVisible];
return YES;
}
It targets iPhone, Landscape only. All works fine on all iPhone devices, but when it runs on iPad, the UIWindow frame is broken.
Any advice?
If I set a view deck controller as the AppDelegate window's root controller, when the app starts in landscape orientation on an iPad, the center view is displayed in it's Portrait orientation size and not resized to Landscape.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
IIViewDeckContoller *rootController = [IIViewDeckController new];
self.window.rootViewController = rootController;
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
}
However, if I create a simple controller as a root controller, and then present the view deck controller from this root controller, then everything seems to display just fine.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *simpleRootController = [UIViewController new];
IIViewDeckContoller *deckController = [IIViewDeckController new];
self.window.rootViewController = simpleRootController;
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
// View Deck Controller seems to have issues when it is the root controller of the main window.
// Presenting it as a modal seems to do the trick.
[simpleRootController presentViewController:self.deckController animated:NO completion:nil];
}
Anyone else run into this issue? Is there a better way to solve this? I do not see the same behavior with the iPhone.
I've seen some people having success with the following method in IIViewDeckController:
- (CGRect) referenceBounds {
if (self.referenceView) {
return self.referenceView.bounds;
}
CGRect bounds = [[UIScreen mainScreen] bounds]; // portrait bounds
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
}
return bounds;
}
However, I'm not seeing anything change. I can't take the OP's advice of presenting the view controller due to some other restrictions.