image popup won't dismiss when I click button - ios

So this brings up an image that is slightly transparent with an "X" button in the top right hand corner that dismisses the image. For some reason it's not working! Any ideas on how to dismiss the image?
Check the button maybe I didn't create the selector correctly...
#define OVERLAY_TAG 997
-(void)showTutorial
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *overlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlay.backgroundColor = [UIColor clearColor];
overlay.userInteractionEnabled = YES;
[keyWindow addSubview:overlay];
UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissTutorial)];
CGFloat border = 10;
CGRect frame = overlay.bounds;
// 20 is the status bar height (sorry for using the number)
frame = CGRectMake(border, border + 20, frame.size.width - border * 2, frame.size.height - border * 2 - 20);
// the black view in the example is probably a scroll view
UIView *blackView = [[UIView alloc] initWithFrame:frame];
blackView.backgroundColor = [UIColor blackColor];
blackView.alpha = 0.7;
[overlay addSubview:blackView];
// add all the subviews for your tutorial
/*UIImage* image = [UIImage imageNamed:#"slide_image_3.png"];
UIImageView* info = [[UIImageView alloc] initWithImage:image];
info.frame = CGRectMake(0, 0, 200, 150);
[blackView addSubview:info];*/
UIImage* image4 = [UIImage imageNamed:#"close_img.png"];
dismissTut = [[UIButton alloc] initWithFrame:CGRectMake(250, 18, 26, 26)];
[dismissTut setBackgroundImage:image4 forState:UIControlStateNormal];
[dismissTut addTarget:self action:#selector(dismissTutorial)
forControlEvents:UIControlEventTouchUpInside];
[dismissTut setShowsTouchWhenHighlighted:YES];
[blackView addSubview:dismissTut];
// make it appear with an animation
[UIView animateWithDuration:0.3
animations:^{blackView.alpha = 0.6;}
completion:^(BOOL finished){[overlay addGestureRecognizer:tapRecognizer];}];
}
-(void)dismissTutorial
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *overlay = [keyWindow viewWithTag:OVERLAY_TAG];
[UIView animateWithDuration:0.3
animations:^{
overlay.alpha = 0.0;
}
completion:^(BOOL finished){
[overlay removeFromSuperview];
}];
}

Add a tag to your view : overlay.tag = OVERLAY_TAG;
-(void)showTutorial
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *overlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlay.tag = OVERLAY_TAG;
.....
}

Related

Add customview in navigation bar like whatsapp

I want to create custom navigation bar like WhatsApp uses to display call indicator in the application as given below.
I have successfully added view like above but it's not responsive because I am not able to detect touch on status bar. I can touch only part below "Touch to return to call".
Code is as given below.
#property (nonatomic) UIView *navigationBarTopView;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (_navigationBarTopView == nil) {
_navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, 60.0)];
[_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15, window.frame.size.width, 10)];
[label setText:#"Touch to return to call"];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
[label setTextAlignment:NSTextAlignmentCenter];
[_navigationBarTopView addSubview:label];
//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[_navigationBarTopView addGestureRecognizer:singleFingerTap];
UITapGestureRecognizer *singleFingerTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[label addGestureRecognizer:singleFingerTap1];
}
[window addSubview:_navigationBarTopView];
I have also tried to add touch on status bar as give below but it doesn't work.
How do I detect touches on UIStatusBar/iPhone
Also navigation bar is not coming downside. I have tried to set keyWindow frame. But that is also not working.
UIStatusBar has higher priority than your application's window, so you won't get any touch event through status bar.
To get touch event through status bar you need a window with higher window level than UIStatusBar.
Try below code:
#import "AppDelegate.h"
#interface AppDelegate ()
#property (nonatomic) UIWindow *buttonWindow;
#property (nonatomic) UIWindow *textWindow;
#property (nonatomic) CGFloat callViewHeight;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (#available(iOS 11.0, *)) {
if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
self.callViewHeight = 55.0f;
} else {
self.callViewHeight = 35.0f;
}
} else {
self.callViewHeight = 35.0f;
}
return YES;
}
-(void)showVideoCallButton{
self.textWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
self.textWindow.windowLevel = self.window.windowLevel + 1;
self.textWindow.hidden = FALSE;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
view.backgroundColor = [UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1];
view.clipsToBounds = TRUE;
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
lblName.textAlignment = NSTextAlignmentCenter;
[lblName setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
lblName.text = #"";
UILabel *lblTouch = [[UILabel alloc] initWithFrame:CGRectMake(0, self.callViewHeight - 20.0f, [UIScreen mainScreen].bounds.size.width, 20.0f)];
lblTouch.textAlignment = NSTextAlignmentCenter;
[lblTouch setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
lblTouch.text = #"Touch to return to call";
lblTouch.textColor = UIColor.whiteColor;
[view addSubview:lblTouch];
[view addSubview:lblName];
[self.textWindow addSubview:view];
self.buttonWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
self.buttonWindow.windowLevel = UIWindowLevelStatusBar + 1;
self.buttonWindow.hidden = FALSE;
UIButton *button = [[UIButton alloc] initWithFrame:lblName.bounds];
[button setTitle:#"" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.buttonWindow addSubview:button];
self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
[UIView animateWithDuration:0.25
animations:^{
self.textWindow.transform = CGAffineTransformIdentity;
self.buttonWindow.transform = CGAffineTransformIdentity;
if (#available(iOS 11.0, *)) {
if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
self.window.frame = CGRectMake(0, self.callViewHeight - 40.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 40.0f);
} else {
self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
}
} else {
self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
}
}];
}
-(void)hideVideoCallButton{
[UIView animateWithDuration:0.25
animations:^{
self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.window.frame = [UIScreen mainScreen].bounds;
} completion:^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
self.buttonWindow.hidden = TRUE;
self.buttonWindow = nil;
self.textWindow.hidden = TRUE;
self.textWindow = nil;
});
}];
}
-(void)buttonTouched{
NSLog(#"Button Touched");
}
#end
I tried in singleViewcontroller and UITabbar Controller also, the sample project I attached here, for status bar Tap action I followed Flar answer
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self createDummyView];
});
}
-(void)createDummyView{
if (_navigationBarTopView == nil) {
float statusBarHeight = [self statusBarHeight];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
_navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, self.navigationController.navigationBar.frame.size.height + statusBarHeight)];
[_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15,width, 10)];
[label setText:#"Touch to return to call"];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setUserInteractionEnabled:YES];
[_navigationBarTopView addSubview:label];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[_navigationBarTopView addGestureRecognizer:singleFingerTap];
[self.navigationController.view addSubview:_navigationBarTopView];
}
-(float) statusBarHeight
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);
}
-(void)handleSingleTap:(UITapGestureRecognizer*)recognizer{
NSLog(#"recognizer == %#", recognizer.view.tag);
}

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.

UIView alpha is disappering after UIButton is clicked

I have a container UIView and a UIView with alpha 0.7f in it, also a custom UIButton with image added to that container UIView. Now whenever I click on the button a big square chunk on transparent UIView disappears and stays that way. I have no idea why this happens. Can anyone help me figure this out?
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
UIView *viewTestOverViewContainer = [[UIView alloc] initWithFrame:window.bounds];
UIView *viewOverlay = [[UIView alloc] initWithFrame:viewTestOverViewContainer.frame];
viewOverlay.backgroundColor = [UIColor blackColor];
viewOverlay.alpha = 0.7;
[viewTestOverViewContainer addSubview:viewOverlay];
float x = (window.bounds.size.width/2)-98;
float y = (window.bounds.size.height/2)-98;
UIView *viewTestPicContainer = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 204, 204)];
viewTestPicContainer.backgroundColor = [UIColor whiteColor];
viewTestPicContainer.layer.cornerRadius = viewTestPicContainer.frame.size.width / 2;
viewTestPicContainer.clipsToBounds = YES;
viewTestPicContainer.backgroundColor = [UIColor whiteColor];
[viewTestPicContainer setContentMode:UIViewContentModeScaleAspectFill];
UIImageView *imgViewTestPic = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 200, 200)];
imgViewTestPic.image = [UIImage imageNamed:#"portrait_eyes_14.jpg"];
imgViewTestPic.layer.cornerRadius = imgViewTestPic.frame.size.width / 2;
imgViewTestPic.clipsToBounds = YES;
imgViewTestPic.backgroundColor = [UIColor whiteColor];
[imgViewTestPic setContentMode:UIViewContentModeScaleAspectFill];
[viewTestPicContainer addSubview:imgViewTestPic];
[viewTestOverViewContainer addSubview:viewTestPicContainer];
UIButton *btnLeftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
btnLeftArrow.frame = CGRectMake(46, 275, 12, 20);
[btnLeftArrow setImage:[UIImage imageNamed:#"popup-left-arrow"] forState:UIControlStateNormal];
[viewTestOverViewContainer addSubview:btnLeftArrow];
UIButton *btnRightArrow = [UIButton buttonWithType:UIButtonTypeCustom];
btnRightArrow.frame = CGRectMake(270, 275, 12, 20);
[btnRightArrow setImage:[UIImage imageNamed:#"popup-right-arrow"] forState:UIControlStateNormal];
[viewTestOverViewContainer addSubview:btnRightArrow];
[window addSubview:viewTestOverViewContainer];
Before:
After clicking the left arrow button:

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;
}

Bring up the UIDatePicker in a subview doesn't animate properly, but bringing it down does

I have made a remide button that bring up this subview, everything works but the animation looks really bad. Here is the code to bring it up:
int height = 255;
//create new view
self.NewView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, height)];
self.NewView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
//add toolbar
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 40)];
toolbar.barStyle = UIBarStyleBlack;
//add button
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:nil action:#selector(dismissCustom:)];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
//add date picker
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDateAndTime ;
self.datePicker.hidden = NO;
self.datePicker.date = [NSDate date];
self.datePicker.frame = CGRectMake(0, 40, 320, 250);
[self.datePicker addTarget:self action:nil forControlEvents:UIControlEventValueChanged];
[self.NewView addSubview:self.datePicker];
//add popup view
[self.NewView addSubview:toolbar];
__block CGRect temp = self.NewView.frame;
temp.origin.y = CGRectGetMaxY(self.view.bounds);
[UIView animateWithDuration:0.5
animations:^{temp.origin.y -= height, self.NewView.frame = temp;}
completion:^(BOOL finished) {[self.view addSubview:self.NewView];}];
Here is the code for the exiting of the subview which works well:
int height = 0;
__block CGRect temp = self.NewView.frame;
temp.origin.y = CGRectGetMaxY(self.view.bounds);
[UIView animateWithDuration:0.5 animations:^{temp.origin.y -= height, self.NewView.frame = temp;} completion:^(BOOL finished) {
[self.NewView removeFromSuperview];}];
Anyone know whats going wrong?
Thanks!
Add NewView to self.view before the animation
CGFloat height = 255.0;
// create new view
self.NewView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.bounds), 320, height)];
// init and add toolbar, button and pickerView as in your code
// ...
// add popup view
[self.view addSubview:self.NewView];
CGRect newViewFrame = self.NewView.frame;
newViewFrame.origin.y -= newViewFrame.size.height;
[UIView animateWithDuration:0.5 animations:^{
self.NewView.frame = newViewFrame;
} completion:nil];

Resources