Dimming the main view when called Popover - iphone

There is a basic form, pressing button call method performSegueWithIdentifier which shows the popover window. How could I black out (dim) the main view window until the popover is active?
I've tried to use the library SVProgressHUD like this:
- (IBAction)publishButtonAction:(id)sender {
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
[self performSegueWithIdentifier:#"fbshareSigue" sender:self];
[SVProgressHUD dismiss];
}
Obtained for a split second - until there is a popover-window.
Where must I insert this code, if I try this?:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"fbshareSigue"]) {
OFAFBShareViewController *svc = (OFAFBShareViewController *) [segue destinationViewController];
UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
svc.postBlock = self.postInitBlock;
[svc setClosePopWindow:[popoverSegue popoverController]];
}
}
- (IBAction)publishButtonAction:(id)sender {
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
[self performSegueWithIdentifier:#"fbshareSigue" sender:self];
}
/* This code should be put in the other method where you are removing the popup.. Because it will remove the dim view. here is the wrong place for this code*/
/* for (UIView *view in [self.view subviews]) {
if (view.tag == 1111) {
[view removeFromSuperview];
}
}*/
Dim doesn't work...
====================
I have View OFAResultViewController that calling Popover (OFAFBShareViewController):
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"fbshareSigue"]) {
OFAFBShareViewController *svc = (OFAFBShareViewController *) [segue destinationViewController];
UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
svc.postBlock = self.postInitBlock;
[svc setClosePopWindow:[popoverSegue popoverController]];
}
}
- (IBAction)publishButtonAction:(id)sender {
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
[self performSegueWithIdentifier:#"fbshareSigue" sender:self];
}
...
In OFAFBShareViewController I'm trying to close the dim view:
...
- (IBAction)cancelButtonAction:(id)sender {
[closePopWindow dismissPopoverAnimated:YES];
for (UIView *view in [self.view subviews]) {
if (view.tag == 1111) {
[view removeFromSuperview];
}
}
...
But it doesn't work again...

You can use the following code, when you display the popup.
In OFAFBShareViewController
- (void) loadView
{
[super loadView];
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
}
After popup removed, delete this view from super view using the tag.
After iOS 5.0 there is a public API method also introduced:
======================================================
To remove the view you can use this code:
for (UIView *view in [self.view subviews]) {
if ([view.tag == 1111]) {
[view removeFromSuperview];
}
}
Leave your Remove Dim View code as it is. Only Change super.view to self.view again.
It'll work for you then.
Hope it works for you...

When you create the popover, add a dim view to the current view, and add the current view as a UIPopoverControllerDelegate of the popover controller:
Add the dimView:
let dimView = UIView(frame: view.frame)
view.addSubview(dimView)
dimView.backgroundColor = UIColor.blackColor()
dimView.alpha = 0.5
dimView.tag = 1234
dimView.userInteractionEnabled = false
view.addSubview(dimView)
Add the origin view as delegate of the popoverController:
popoverViewController!.delegate = self
Then, in order to remove the dimView when the popover is dismissed, set the current view as implementing the UIPopoverControllerDelegate, and implement the popoverControllerDidDismissPopover func. Inside this function, remove the dimView:
extension MyOriginViewController: UIPopoverControllerDelegate {
func popoverControllerDidDismissPopover(popoverController: UIPopoverController) {
for subView in view.subviews {
if subView.tag == 1234 {
subView.removeFromSuperview()
}
}
}
}

Related

Hide View by click on any part of the screen in iphone

I have a View which is the subView of the main view in iphone app I want that when subView is shown and user taps on part of the screen except the subView then subView should hide.
Here is the code which I got but it does not hide it :
UITapGestureRecognizer *tapGR;
tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)] autorelease];
tapGR.numberOfTapsRequired = 1;
[self.View addGestureRecognizer:tapGR];
// Add a delegate method to handle the tap and do something with it.
-(void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) {
// handling code
[myViewSheet removeFromSuperView];
}
}
implement UIGestureRecognizerDelegate protocol:
.h:
#interface YourView : UIView<UIGestureRecognizerDelegate>
.m:
#implementation YourView{
UIView * subview;
}
...
subview = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
[self addSubview:subview];
UITapGestureRecognizer *tapGR;
tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tapGR.numberOfTapsRequired = 1;
tapGR.delegate = self;
[self addGestureRecognizer:tapGR];
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view == subView)
{
return NO;
}
else
{
return YES;
}
}
You probably need to set userInteractionEnabled = YES on your main view (The one to which you are attaching the gesture recognizer.)

How can I programmatically segue with tapping on an Image?

I've implemented 2 ImageViews with images and tapGestures, this works fine, now I want to segue to another ViewController dependent on which image I've tapped. I don't know how I have to declare my ImageView which ViewController it should call.
so my Code is:
-(void) initImageViews{
self.navigationItem.title = #"Menu";
UIImage *serviceImage = [UIImage imageNamed:#"Service.png"];
self.serviceImageView = [[UIImageView alloc] initWithImage:serviceImage];
[self.serviceImageView setFrame:CGRectMake(20, 20, 48,48 )];
[self.view addSubview:self.serviceImageView];
[self.view setBackgroundColor:[UIColor blackColor]];
UIImage *industryImage = [UIImage imageNamed:#"Industry.png"];
self.industryImageView = [[UIImageView alloc] initWithImage:industryImage];
[self.industryImageView setFrame:CGRectMake(73,20,48,48)];
[self.view addSubview:industryImageView];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.industryImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView addGestureRecognizer:singleTap];
[self.industryImageView addGestureRecognizer:singleTap];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {
NSLog(#"Tapped!");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Service"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}if ([segue.identifier isEqualToString:#"Industry"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}
}
}
Thanks in advance :)
[self performSegueWithIdentifier:IdentifierForTheSegueiFromStoryBoard sender:nil];
EDIT: the seque must exist in the storyboard and must have an identifier.
If you don't want/need to use segues you could use:
if (i want controller A) {
[self presentViewController:ControllerA animated:YES completion:nil];
} else if (i want controller B)
[self presentViewController:ControllerB animated:YES completion:nil];
}
Create a seague in the storyboard with the identifier.then programatically call a segue like this
[self performSegueWithIdentifier: #"myidentifier" sender: self];

Multiple gestureRecognizers on views- one shouldn't pass touch

On parentClass(inheriting from UIView) i have:
[self addGestureRecognizer:_tapGesture]; // _tapGesture is UITapGestureRecognizer, with delegate on parentClass
On someClass:
[_myImageView addGestureRecognizer:_imageViewGestureRecognizer]; // _imageViewGestureRecognizer is UITapGestureRecognizer, with delegate on someClass
The problem is that when i tap on myImageView both gesture recognizers are firing. I want only _imageViewGestureRecognizer to work.
I've tried:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch {
UIView *gestureView = recognizer.view;
CGPoint point = [touch locationInView:gestureView];
UIView *touchedView = [gestureView hitTest:point withEvent:nil];
if ([touchedView isEqual:_imageViewGestureRecognizer]) {
return NO;
}
return YES;
}
But it ofc doesn't take upon consideration gesture recognizer from super class.
I did this little test and it worked perfectly...
#implementation View
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped1)];
[self addGestureRecognizer:tap];
UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
img.userInteractionEnabled = YES;
img.frame = CGRectMake(0, 0, 100, 100);
[self addSubview:img];
UITapGestureRecognizer* tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped2)];
[img addGestureRecognizer:tap2];
return self;
}
-(void)tapped1 {
NSLog(#"Tapped 1");
}
-(void)tapped2 {
NSLog(#"Tapped 2");
}
#end
What you wanted was iOS's default behaviour. Once a subview has taken care of a touch, its superview won't receive the touch anymore. Did you set userInteractionEnabled on the imageView?

Handle UIButton in Child View Controller of Container View Controller

I am working on creating custom Container ViewController class and am able to create 5 sub viewcontrollers and able to move from one child view controller to other child using the following API:
transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:0
animations:^{
}
completion:^(BOOL finished) {
}];
Child view controllers are occupying part of Parent View Controller. Each time when user swipe I am making previous controller goes off and present controller comes on displayable area.
Now I have buttons on child viewcontrollers and when i click on them I am not getting events or trigger to the Action method of Child View Controller. But Parent View Controller is getting callbacks for its buttons even currently childview is getting displayed. Adding the code snippet below (though it is little bigger, this will help you to analyze if I am doing any mistake).
Parent ViewController:
- (void) handleSwipeGestureLeft:(UISwipeGestureRecognizer *)gesture
{
if (showPopupValue) {
return;
}
NSInteger index = [arrayOfChildVCs indexOfObject:selectedViewController];
index = MIN(index+1, [arrayOfChildVCs count]-1);
UIViewController *newSubViewController = [arrayOfChildVCs objectAtIndex:index];
endFrame.origin.x = -1024;
startOfNewVCFrame.origin.x = 1024;
[self transitionFromViewController:selectedViewController toViewController:newSubViewController];
selectedViewController = newSubViewController;
}
- (void) handleSwipeGestureRight:(UISwipeGestureRecognizer *)gesture
{
if (showPopupValue) {
return;
}
NSInteger index = [arrayOfChildVCs indexOfObject:selectedViewController];
index = MAX(index-1, 0);
UIViewController *newSubViewController = [arrayOfChildVCs objectAtIndex:index];
endFrame.origin.x = 1024;
startOfNewVCFrame.origin.x = -1024;
[self transitionFromViewController:selectedViewController toViewController:newSubViewController];
selectedViewController = newSubViewController;
}
- (void) populateChildVCList
{
if (self.currentactivity == EPI_Normal)
{
arrayOfImageNames = [[NSArray alloc]initWithObjects:#"AD_Initiate_SPI_Normal.jpg", #"AD_Initiate_CPI_Normal.jpg", #"AD_Initiate_EAC_Normal.jpg", #"AD_Initiate_PV_Normal.jpg", #"AD_Initiate_EV_Normal.jpg", nil];
}
else
{
arrayOfImageNames = [[NSArray alloc]initWithObjects:#"AD_Initiate_SPI_Trigger.jpg", #"AD_Initiate_CPI_Trigger.jpg", #"AD_Initiate_EAC_Trigger.jpg", #"AD_Initiate_PV_Trigger.jpg", #"AD_Initiate_EV_Trigger.jpg", nil];
isTriggerOn = YES;
}
arrayOfChildVCs = [[NSMutableArray alloc] init];
[arrayOfChildVCs addObject:[[PopupDummyViewController alloc]initWithNibName:#"PopupDummyViewController" bundle:nil]]; // Note: Add one dummy VC to initiate swap
for (int vcIndex = 0; vcIndex < [arrayOfImageNames count]; vcIndex++)
{
PiSLAComplianceViewController *childPopupController = [[PiSLAComplianceViewController alloc]initWithNibName:#"PiSLAComplianceViewController" bundle:nil];
childPopupController.popUpImageName = [arrayOfImageNames objectAtIndex:vcIndex];
childPopupController.imageIndex = vcIndex;
childPopupController.isTriggerOn = isTriggerOn;
[arrayOfChildVCs addObject:childPopupController];
}
selectedViewController = [arrayOfChildVCs objectAtIndex:0];
selectedViewController.view.frame = CGRectMake(0, 100, 100, 511); // Took the imageview coordinates
// Add the dummy view controller to Container View Initially
[self addChildViewController:selectedViewController];
[self.view addSubview:selectedViewController.view];
// notify it that move is done
[selectedViewController didMoveToParentViewController:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[super viewDidLoad];
UISwipeGestureRecognizer *swipeGestureRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureRight:)];
[swipeGestureRecognizerRight setDirection:UISwipeGestureRecognizerDirectionRight];
[ self.view addGestureRecognizer:swipeGestureRecognizerRight];
UISwipeGestureRecognizer *swipeGestureRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeGestureLeft:)];
[swipeGestureRecognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[ self.view addGestureRecognizer:swipeGestureRecognizerLeft];
endFrame = deliverableimageview.frame;
startOfNewVCFrame = deliverableimageview.frame;
[self populateChildVCList]; // To Create list of child view controllers and loads one invisible dummy view controller to be ready for swiping
}
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
if (fromViewController == toViewController)
{
// cannot transition to same
return;
}
// animation setup
toViewController.view.frame = startOfNewVCFrame;
// notify
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
// transition
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:0
animations:^{
toViewController.view.frame = fromViewController.view.frame;
fromViewController.view.frame = endFrame;
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
}

tabBarController set to nil when changing view but always in the window

I'm currently using a tableview inside a navigation controller, inside a tab bar controller. When i'm on the detail view of the table view, i want to "swipe" between Detail view, so i have implemented a UISwipeGesture with UIViewAnimations to swipe right/left.
Everything is working, but I have a little problem... When i'm on a Detail View loaded from the table view, the self.tabBarController element is here, i have my tab bar so i can add an actionsheet on it. But when i swipe, the self.tabBarController is nil now... I don't know why... Maybe because of the navigation Controller ?
Here is my code :
- (id) init
{
if (self = [super init]) {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, fullScreenRect.size.width, 367.)];
scrollView.contentSize=CGSizeMake(320,100);
scrollView.delegate = self;
self.view=scrollView;
self.myView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 367.)];
self.ButtonSizeM.tag = 0;
self.ButtonSizeP.tag = 0;
self.ContentIV = [[UIImageView alloc] init];
}
return self;
}
- (void) handleSwipeLeft {
if (!((self.ancient.tableView.indexPathForSelectedRow.section == ([self.ancient numberOfSectionsInTableView:self.ancient.tableView] - 1)) && (self.ancient.tableView.indexPathForSelectedRow.row == [self.ancient tableView:self.ancient.tableView numberOfRowsInSection:(self.ancient.tableView.indexPathForSelectedRow.section)]-1)))
{
NSIndexPath *whereToGo;
if (self.ancient.tableView.indexPathForSelectedRow.row == [self.ancient tableView:self.ancient.tableView numberOfRowsInSection:(self.ancient.tableView.indexPathForSelectedRow.section)]-1) {
whereToGo = [NSIndexPath indexPathForRow:0 inSection:(self.ancient.tableView.indexPathForSelectedRow.section)+1];
}
else {
whereToGo = [NSIndexPath indexPathForRow:(self.ancient.tableView.indexPathForSelectedRow.row)+1 inSection:self.ancient.tableView.indexPathForSelectedRow.section];
}
CCFASimpleDetail *nextView = [[CCFASimpleDetail alloc] init];
nextView = [self.ancient tableView:self.ancient.tableView prepareViewForIndexPath:whereToGo];
[nextView performSelectorOnMainThread:#selector(affichageEnPlace) withObject:nil waitUntilDone:YES];
float width = self.myView.frame.size.width;
float height = self.myView.frame.size.height;
[nextView.view setFrame:CGRectMake(width, 0.0, width, height)];
[self.view addSubview:nextView.view];
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
[nextView.view setFrame:self.view.frame];
[self.myView setFrame:CGRectMake(-width, 0.0, width, height)];
} completion:^(BOOL finished) {
[self.myView removeFromSuperview];
[self.ancient.tableView selectRowAtIndexPath:whereToGo animated:NO scrollPosition:UITableViewScrollPositionTop];
}
];
}
}
I you could help me to find my tab bar after swiping :) ?
When you add the view here:
[self.view addSubview:nextView.view];
the UINavigation controller does not know about it, and does not set the navigationBar nad tabBar properties for you.
What you can do is have the view get its superview (which will be self.view), then ask that view for the tabBarController and tabBar from that.
I just solved my problem by adding
[self.ancient.navigationController popViewControllerAnimated:NO];
[self.ancient.navigationController pushViewController:nextView animated:NO];
on the completion block !