How to avoid corrupted borders when presenting a modal controller in a UIPopOverController - cocoa-touch

I'm having an issue when presenting a modal view controller within a popover using the modal presentation style UIModalPresentationCurrentContext.
Something strange is happening to the border of the popover, here are some before/after screenshots:
Before presenting the modal controller:
And now once the modal controller has been displayed:
See how the border width on the left and top has changed? When the modal controller is dismissed the border width stays corrupted but if the whole popover is dismissed it displays correctly until the modal controller is shown again.
Here is the code we're using in a sample project that we've created to demonstrate the problem:
- (IBAction)showModalViewButtonTapped:(id)sender
{
UIViewController *viewController = [[UIViewController alloc] init];
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismissButton.frame = CGRectMake(30, 30, 100, 25);
[dismissButton setTitle:#"Dismiss" forState:UIControlStateNormal];
[dismissButton addTarget:self action:#selector(dismissButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[viewController.view addSubview:dismissButton];
viewController.view.backgroundColor = [UIColor redColor];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
Has anyone seen this before? any ideas?

Related

how to dim background view

trying to dim background view when I work with custom modal view controller I set background color to grey color with 0.5 opacity:
- (void)rulesTapped:(id)sender
{
RulesVC *rulesVC = [[RulesVC alloc] init];
self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.5];
rulesVC.modalPresentationStyle = UIModalPresentationCustom;
rulesVC.transitioningDelegate = self;
[self presentViewController:rulesVC animated:YES completion:nil];
}
but when I then try to set it back to whiteColor (or yellow) while dismissing the modal VC, this dimming keeps in place:
- (void) gotItTapped:(id)sender
{
[self.presentingViewController.view setBackgroundColor:[UIColor yellowColor]];
[self dismissViewControllerAnimated:YES completion:nil];
}
Why? And is there way to dim background (and then remove dimming) view without adding another overlaying view but just operating with existing view?
I don't think you can achieve it without adding another element to your UIViewController. I normally insert an UIView which is setHidden:YES and alpha 0.0f and blackColor as backgroundColor. When showing something, I always set it Hidden:NO and animate it to opacity 0.6f.
[UIView animateWithDuration:0.5f animations:^{
[self.view setAlpha:0.6];
}];
This way it appears not immediately and looks much smoother.
I actually needed this to be able to put some other UIView in dim view, and that every click outside this dimview, would dismiss both built-in UIView and dimView. What I've discovered, is that for this the best is to imitate a button which is removed from superview upon click:
self.dimView = [UIButton buttonWithType:UIButtonTypeCustom];
self.dimView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2f];
self.dimView.frame = CGRectMake(0,
0,
self.view.frame.size.width,
self.view.frame.size.height);
[self.dimView addTarget:self
action:#selector(removeDimView)
forControlEvents:UIControlEventTouchUpInside];

iOS7 - Navigation Controller - Custom Back Button

I need to use custom Back button image for my new iOS 7 app. When I use Storyboard and add image by using Attributes inspector, this is what XCode renders for me:
Does anyone know why the Navigation controller behaves this way? I'm regularly putting #2x PNG image into Bar Button Item(under Navigation Item) in my Table View.
#interface MEDevicesViewController : UITableViewController
In my application, I use ECSlidingViewController and I have Table View within Navigation Controller. This is on my navigation controller.
Any help appreciated...
UIButton * button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 51, 31)];
[button setImage:[UIImage imageNamed:#"back_btn.png"]forState:UIControlStateNormal];
[button addTarget:self
action:#selector(backbtnpressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
-(IBAction)backbtnpressed:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

Detecting background tap

I'm busy with an app you need to touch a button, but when u touch outside the button (on the background of the app screen) i want to display an alert.
Does anyone know how to detect an tap on the background
Button:
MyButton = [UIButton buttonWithType:UIButtonTypeCustom];
MyButton.frame = CGRectMake(0, 0, 100, 100);
[MyButton setImage:[UIImage imageNamed:#"tap.png"] forState:nil];
[self.view addSubview:MyButton];
[MyButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
You could add a gesture recogniser to the view.
E.g.
// in viewDidLoad:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(backgroundTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapRecognizer];
- (void)backgroundTapped:(UITapGestureRecognizer*)recognizer {
// display alert
}
What you could also try is having a full size UIView behind the button that has a gesture recogniser on it:
// in viewDidLoad:
UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
backgroundView.backgroundColor = [UIColor clearColor];
backgroundView.opaque = NO;
[self.view addSubview:backgroundView];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(backgroundTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[backgroundView addGestureRecognizer:tapRecognizer];
- (void)backgroundTapped:(UITapGestureRecognizer*)recognizer {
// display alert
}
This might work better than adding the gesture recogniser to self.view.
You can create a custom subclass of a UIView, make it transparent but with touch handlers, and place a full size instance of that new view subclass underneath your button. You can then have that subview send a message to your main view controller (via delegation) whenever it see a touch down event. Since the button is on top of this subview, the subview won't do anything if the button is tapped.
I looked at the solution in the first answer and tested it. It did not work for me. The gesture recognizer captured my button & other UI element touches (I'm loading from nib)
A window delivers touch events to a gesture recognizer before it delivers
them to the hit-tested view attached to the gesture recognizer. Generally,
if a gesture recognizer analyzes the stream of touches in a multi-touch
sequence and does not recognize its gesture, the view receives the full
complement of touches
I used a slightly different solution:
UIButton *invisibleBtn = [[UIButton alloc] initWithFrame:self.view.bounds];
invisibleBtn.titleLabel.text = #"";
invisibleBtn.backgroundColor = [UIColor clearColor]; // no tap events if this is not set, bizarre
[invisibleBtn addTarget:self action:#selector(backgroundTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:invisibleBtn];
[self.view sendSubviewToBack:invisibleBtn];

Present formsheet modal ViewController using horizontal flip

I currently show a modal UIViewController in the formSheet style that was presented with the coverVertical animation. I am trying to present another modal UIViewController from it, using currentContext for the presentation style and flipHorizontal for the animation style. It does work, but there is a solid white white background behind the flip as it occurs. Any advice on how to successfully present another formsheet modal UIViewController using the flipHorizontal style from a pre-existing modal UIViewController in the formSheet style would be appreciated please! Thanks
Code:
// Loading of first modalVC
- (void)showModalVC {
Modal1VC *modal1VC = [[Modal1VC alloc] init];
modal1VC.modalPresentationStyle = UIModalPresentationFormSheet;
modal1VC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:modal1VC animated:YES];
[modal1VC release];
modal1VC.view.superview.bounds = CGRectMake(0, 0, 400, 400);
}
// Loading of second modalVC in Modal1VC
- (void)buttonTapped:(id)sender {
UIViewController *modal2VC = [[UIViewController alloc] init];
modal2VC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modal2VC.modalPresentationStyle = UIModalPresentationCurrentContext;
modal2VC.view.backgroundColor = [UIColor greenColor];
[self presentModalViewController:modal2VC animated:YES];
[modal2VC release];
modal2VC.view.superview.bounds = CGRectMake(0, 0, 400, 400);
}
UIViewController *presentingViewController = //allocate your VC
[modalViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
UIViewController *modalViewController = //your modal VC
[presentingViewController presentModalViewController:modalViewController animated:YES];
This is all the code you need ;)
It sounds like your transition is working, but the white background shown on the flip transition is the problem?
During the flip transition, the white background that is shown is actually the window. You can change the color that is shown by setting the backgroundColor property of window in the AppDelegate.
That would seem to be a limitation of presentModalViewController:animated:.
Instead of [self presentModalViewController:modal2VC animated:YES], you may be able to get the effect you want using UIView animations. Perhaps something like this:
[UIView transitionWithView:self.view.window
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self presentModalViewController:modal2VC animated:NO];
} completion:NULL];

Objective C: Using code to add a toolbar to a UITableView (within a Navigation Controller)

I managed to add a toolbar at the bottom of my UITableView using the code below:
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
toolbar.frame = CGRectMake(0, 436, 320, 50);
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
[self.navigationController.view addSubview:toolbar];
However when I try to switch back to the first page of the navigation controller, the toolbar at the bottom of the page is still displayed. How can I ensure that the toolbar is only shown on the UITable View and not any other views in the navigation controller?
Thanks in advance.
Zhen
In your TableViewController implement:
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.toolbar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
self.navigationController.toolbar.hidden = YES;
}