UIWindow Slide Animation - ios

I am displaying a new UIWindow and I'm wanting it to slide in from the left and slide into the left side when it's dismissed. How can I animate the showing and removing of a UIWindow?
This is how I'm currently showing my new UIWindow.
- (void)showMenu
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UIButton *closeMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeMenuButton setFrame:CGRectMake(250, 10, 50, 50)];
[closeMenuButton setImage:[UIImage imageNamed:#"close"] forState:UIControlStateNormal];
[closeMenuButton addTarget:self action:#selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];
blurredView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[blurredView setBarStyle:UIBarStyleBlack];
MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:#"MenuTableViewController" bundle:nil];
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);
menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:blurredView];
[blurredView addSubview:closeMenuButton];
[blurredView addSubview:menu.view];
[menuWindow makeKeyAndVisible];
}
Maybe Like this?
menuWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, 0, screenHeight)];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:blurredView];
[blurredView addSubview:closeMenuButton];
[blurredView addSubview:menu.view];
[menuWindow makeKeyAndVisible];
[UIView animateWithDuration:0.5 animations:^{
menuWindow.frame = screenRect;
blurredView.frame = screenRect;
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);
}
completion:^(BOOL finished) {
}];

You can animate a UIWindow like any other view (using UIView animation methods).
A few things to remember. You need to retain a window for it to stay on screen. Also, you need to remember that windows live in screen coordinates, not in view coordinates, so you need to take into consideration the current interface orientation before starting an animation.
In the example above, I see you are creating a window, making it key and visible ... and nada. You need to retain the window.
Also, you are using the API incorrectly. Once you set a root view controller, the framework is responsible for the view. You add it as a subview again, and that can mess things. You really shouldn't add subviews to a window directly, just add them to the view controller's view hierarchy, and let that handle it.

Related

UIWindow addSubview handle events

I want to build a custom alert by UIViewController and add its view as a subview to the current window. The view displays very well, but I can't handle any touch events.
I have tried many times to add a button to this viewcontroller and add a target. I have also tried adding a UITapGestureRecognizer and set view's userInteractionEnabled to true, but this also failed even when I cleared all subviews just left the button.
Have I missed something?
Here is the code:
CustomAlert Viewcontroller:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
backImage = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 300) / 2, (self.view.frame.size.height - 250) / 2, 300, 250)];
backImage.image = [UIImage imageNamed:#"AlertBackground"];
[self.view addSubview:backImage];
confirmButton = [[UIButton alloc]initWithFrame:CGRectMake( backImage.frame.origin.x + 100 , backImage.frame.origin.y + 250 - 40, 100, 26)];
[self.view addSubview:confirmButton];
[confirmButton addTarget:self action:#selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
confirmButton.backgroundColor = [UIColor redColor];
[confirmButton setTitle:#"click me" forState:UIControlStateNormal];
}
-(void)confirmButtonClick:(UIButton*)sender{
[self.view removeFromSuperview];
}
-(void)show{
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window addSubview:self.view];
}
Method call custom alert:
CustomAlert * alert = [[CustomAlert alloc]init];
[alert show];
Try to rewrite your -show{} method with these
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window.rootViewController.view addSubview:self.view];
UPDATE:
And if previous don't help, try to overwrite
-(void)show{
UIWindow * window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
[window.rootViewController addChildViewController:self];
[window.rootViewController.view addSubview:self.view];
[self didMoveToParentViewController:window.rootViewController];
}
Those three methods establish parent-child relations.

Two different approaches to subviews

Regarding subviews in ios, I have a view that covers the whole screen, then I create and import another view which is just a red square on top of it. I was wondering if there is any difference or advantadge between these two approaches:
Approach 1:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.view addSubview:firstView];
and approach 2:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[myView addSubview:firstView];
The only difference being that in the first case I add both views as subviews of the main property view whereas in the second case I add the second view as a subview of the first view. They look the same on screen.
Thanks
The difference in my opinion that on the first approach if you change any of (myView) constraints , firstFrame will not change but in the second approach it will.

Subview on UITableView

I've added a subview to my table view controller. The subview has three subviews (a close button, a toolbar used to blur the main view, and a table view). The problem is that you are still able to see a bit of the main view at the bottom of the subview. I have tried initializing the subview with this frame, contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];, and that fixes the issue, but then if I scroll down on the parent view's table view, the subview can't be seen. How can I fix this?
- (void)showMenu
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UIColor *kfbBlue = [UIColor colorWithRed:8.0/255.0f green:77.0/255.0f blue:139.0/255.0f alpha:1];
contentView = [[UIView alloc]initWithFrame:self.tableView.bounds];
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
contentView.backgroundColor = [UIColor clearColor];
UIButton *closeMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeMenuButton setFrame:CGRectMake(275, 10, 40, 40)];
[closeMenuButton setImage:[UIImage imageNamed:#"close"] forState:UIControlStateNormal];
[closeMenuButton addTarget:self action:#selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];
blurredView = [[UIToolbar alloc]initWithFrame:contentView.bounds];
[blurredView setBarStyle:UIBarStyleBlack];
[blurredView setBarTintColor:kfbBlue];
MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:#"MenuTableViewController" bundle:nil];
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight);
[self.view addSubview:contentView];
[contentView addSubview:blurredView];
[self addChildViewController:menu];
[contentView addSubview:menu.view];
[contentView addSubview:closeMenuButton];
self.navigationController.navigationBarHidden = YES;
self.tableView.scrollEnabled = NO;
}

New UIWindow Won't Display

I am trying to create a new UIWindow to cover the entire screen, including the status bar. I get it to appear when a button is pressed but it just quickly flashes on the screen and goes away. What am I doing wrong?
MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:#"MenuTableViewController" bundle:nil];
UIWindow *menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:menu.view];
[menuWindow makeKeyAndVisible];
menuWindow.hidden = NO;
I got it working by adding #property (nonatomic, strong) UIWindow *menuWindow; to my header file. Here is what my showMenu and closeMenu methods look like.
- (void)showMenu
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
UIButton *closeMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeMenuButton setFrame:CGRectMake(250, 10, 50, 50)];
[closeMenuButton setImage:[UIImage imageNamed:#"close"] forState:UIControlStateNormal];
[closeMenuButton addTarget:self action:#selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];
blurredView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[blurredView setBarStyle:UIBarStyleBlack];
MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:#"MenuTableViewController" bundle:nil];
menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);
menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
menuWindow.backgroundColor = [UIColor clearColor];
menuWindow.windowLevel = UIWindowLevelStatusBar;
menuWindow.rootViewController = menu;
[menuWindow addSubview:blurredView];
[blurredView addSubview:closeMenuButton];
[blurredView addSubview:menu.view];
[menuWindow makeKeyAndVisible];
}
- (void)closeMenu
{
menuWindow.hidden = YES;
menuWindow = nil;
}

Placing a fixed position toolbar in a Split View Controller's root view

I am having a problem placing a tool bar in the root view controller for a split view IPad app. I tried reducing the table view size and placing a toolbar under it, but the frame adjustment doesn’t take and the toolbar ends up scrolling with the table and not staying at the bottom of the screen like I want.
Here is the code that I am using in viewDidLoad:
self.tableView.frame = CGRectMake(0, searchBar.bounds.size.height, 320, 655 - searchBar.bounds.size.height);
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlack;
toolbar.frame = CGRectMake(0, 655, 320, 50);
[self.view addSubview:toolbar];
Thanks.
Found a nice solution to this problem, here is the code:
[super viewWillAppear: animated];
toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleBlack;
toolbar.frame = CGRectMake(0, 0, 320, 49);
[toolbar sizeToFit];
CGFloat toolbarHeight = 49;
CGRect rootViewBounds = self.parentViewController.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];

Resources