why the animation in the didFinishLaunchingWithOptions failed? - ios

I just want to diaplay the view with animation in the didFinishLaunchingWithOptions, my codes are like the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect finalRect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
UIImage *image = [UIImage imageNamed:#"122.jpg"];
UIButton* pic = [[UIButton alloc] initWithFrame:finalRect];
[pic setBackgroundImage:image forState:UIControlStateNormal];
[pic setHidden:true];
pic.center = CGPointMake(finalRect.size.width / 2, finalRect.size.height / 2);
UIViewController* controller = [[UIViewController alloc] init];
[controller setView:pic];
[self.window setRootViewController:controller];
[self.window makeKeyAndVisible];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.5];
[pic setHidden:false];
[UIView commitAnimations];
return YES;
}
but the animation does not work at all. The subview with a picture just showed in the screen suddenly. But if I use a button to trigger the animation codes, the view will appear with animation as supposed to. Is there any restricts on the didFinishLaunchingWithOptions?
PS:
This problem is resolved by moving the animation codes into the viewDidAppear proc of the controller just like rokjarc said below.
But there is another solution for this by execute the animation codes in a delayed routine called from the didFinishLaunchingWithOptions like:
[self performSelector:#selector(executeAnimation) withObject:nil afterDelay:1];

Your button gets covered by controller.view which is also loaded in window.view on top of your pic.
You can solve this by moving the code to controller::viewDidAppear.
If you decide to go this way don't forget to add the button to controller.view instead of appDelegate.window.
Since it looks that you only want to show this animation at application launch you could set add a BOOL property called showAnimation to controller. Set this property to YES in didFinishLaunchingWithOptions: and to NO at the end of controller::viewDidAppear.
This way you can conditionally (if (self.showAnimation)...) show the desired animation only once (in controller::viewDidAppear).

Related

Status bar issue using xib

I have created two ViewCrontroller using xib and set orientation of UIViews is LandscapeLeft.
and than add this code to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The following is my ideal result:
I'd like the window under the status bar in ios 7,
The first view, the result is what I want to get, however, when it jumps to the second view, there is a problem! The window move 20px to the left. how to fit it into the normal view?
3.Most impornatant condition is Autolayout is "TRUE".
my testing code:
https://www.dropbox.com/s/bj9wg4hwar1t8y0/statusbartesting.zip
Can anyone help to solve this problem?
You don't need to change window frame in AppDelegate.
I'll tell you code, please follow it
Appdelegate.m
- (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];
self.mxfirstciewcontroller = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.mxfirstciewcontroller];
[(UINavigationController *)self.window.rootViewController setNavigationBarHidden:YES];
return YES;
}
In firstViewController.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view setFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
}
In secondViewController.m
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.view setFrame:CGRectMake(20, 0, self.view.frame.size.width, self.view.frame.size.height)];
}

Issues Starting in Landscape with IIViewDeckController as RootController on iPad

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.

How do I add a dark screen that covers all other views (including navigation bar and status bar)?

I want to add a dark screen over all my views, and then present a notification window above it, like in Tweetbot 3:
However, I'm not totally sure how this is accomplished.
Adding a dark view with the size of the screen as its frame does not work, as below: (navigation bar and status bar aren't covered)
UIView *darkOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
darkOverlay.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self.view insertSubview:darkOverlay behindSubview:self.view];
This covers everything except the status bar (so it comes pretty close, but as I have light status bar content it's still very jarring):
[[[UIApplication sharedApplication] keyWindow] addSubview:darkOverlay];
So how is something like this accomplished?
You can try this method
UIWindow* window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.windowLevel = UIWindowLevelAlert;
window.opaque = NO;
[window addSubview:someView];
// window has to be un-hidden on the main thread
[window makeKeyAndVisible];
Where "view" - is your custom popup.
You have to do this:
CGRect screenRect = [[UIScreen mainScreen] bounds];
_darkCoverView = [[UIView alloc] initWithFrame:screenRect];
_darkCoverView.backgroundColor = [UIColor blackColor];
_darkCoverView.alpha = 0.5;
[[[[UIApplication sharedApplication] delegate] window] addSubview:_darkCoverView];
It works for me, and the status bar is covered as well :)
You can try to simply present it modally, as modal view controllers go above nav controllers.
TransparancyViewController *vc=[[TransparancyViewController alloc]initWithNibName:#"TransparancyViewController" bundle:nil] ;
vc.view.backgroundColor = [UIColor clearColor];
navController.view.alpha = 0.3;
navController.modalPresentationStyle = UIModalPresentationCurrentContext;
[navController presentViewController:vc animated:NO completion:nil];
Small sample project for using with Storyboards.. http://cl.ly/442C11341k2G
When you create the darkOverlayView it may be better to specify the actual bounds of the view, which should fill the whole screen (width,height for iPhone 5 used).
UIView *darkOverlay = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 568)];
If your notification object is also a UIView, bring this to the front.
[self.view addSubView:darkOvelay]
[self.view bringSubviewToFront:notificationView];
I hope this solves your problem, cheers, Jim.

initWithFrame is given empty frame

I'm attempting to use a UINavigationController that utilizes a custom subclass of UINavigationBar. However, I'm running into a problem with initWithFrame. When initWithFrame is called, I print out the dimensions and it shows a 0.0 width and 0.0 height. But, when I print out the same frame in -(void) layoutSubviews inside of the UINavigationBar subclass, the frame is correctly set. Why is initWithFrame receiving a zero-sized frame, and what can I do to fix this? It doesn't sound correct to be manually calling initWithFrame or anything like that, since UINavigationController should take care of that. Here is the relevant code:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
CoolViewController *vc = [[CoolViewController alloc] init];
[navController pushViewController:vc animated:YES];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
CustomNavigationBar.m
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
NSLog(#"initWithFrame with frame: %f, %f", frame.size.width, frame.size.height); // This prints "initWithFrame with frame: 0, 0"
}
return self;
}
Thank you!
I believe the navigation controller doesn't actually set the frame size to a specified value. Instead, it asks the navigation bar for its sizeThatFits: and uses that. By definition, that can't be done and passed to initWithFrame:. So, you want to use a combination of sizeThatFits:, setFrame: and layoutSubviews to add, size and reset your navigation bar content.

programmatically add a UIButton to rootview, what am I doing wrong?

I have freshly setup app, just with a window an a root view (from the iOS single view application template, provided by XCode).
Now I try to add a button to it.
The according code looks like this:
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIButton* button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button0.frame = CGRectMake(140, 230, 40, 20);
[button0 setTitle:#"Exit" forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(action) forControlEvents:UIControlEventTouchUpInside];
[self.viewController.view addSubview:button0];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
else
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
When I start the app, I can only see the empty view.
However, when comment out the line, which adds the view as root to the window and instead directly add the button to the window, then I can see the button just fine.
So why isn't this working with the view?
The problem is that, you are adding button0 as subview of self.viewController.view even before allocating the viewController.
By the time you call addSubview: method, self.viewController is nil. So, the button is not added to the viewController. You should add the button after you allocate the viewController.
self.viewController = [[ViewController alloc]...
[self.viewController.view addSubview:button0];
At first, create the root viewcontroller, later add subviews to it. You did it the other way round.

Resources