Objective-C Modal View Controller with Transparent Background - objective-c

I wanted to create a modal View Controller that would have a transparent background but when I tried setting the alpha of the background to 0.5 it just made the view behind it completely black. This is because the background views are being removed after the transition.
self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[self setModalPresentationStyle:UIModalPresentationFullScreen];
[self setDefinesPresentationContext:YES];
Then presenting View Controller Code
SecondViewController *alertView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:alertView animated:YES completion:nil];
please look at the link
image/linke.gif

Try this code in the action of the button in the firstViewController to change the view controller and present a new class secondViewController
secondViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
VC.view.backgroundColor=[UIColor colorWithWhite:0 alpha:0.1f];
VC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
[self presentViewController:VC animated:NO completion:nil];

Related

UIPopoverPresentationController can not be dismissed on iPhone

I'm implementing a CABTMIDICentralViewController (Apple's pre-fab BTLE MIDI configuration panel). The code below is Apple's sample code - unmodified.
It works perfectly on iPad, but on iPhone/iPod it results in an uncloseable fullscreen view. The code clearly creates a Done button, but it isn't shown on the devices.
The common answer is "you need a UINavigationController", but there is one being made in this code. So I'm not sure what else is missing?
- (void)doneAction:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)configureCentral:(id)sender
{
CABTMIDICentralViewController *viewController [CABTMIDICentralViewController new];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// this will present a view controller as a popover in iPad and modal VC on iPhone
viewController.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneAction:)];
navController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popC = navController.popoverPresentationController;
popC.permittedArrowDirections = UIPopoverArrowDirectionAny;
popC.sourceRect = [sender frame];
UIButton *button = (UIButton *)sender;
popC.sourceView = button.superview;
[self presentViewController:navController animated:YES completion:nil];
}
You will have to implement the UIPopoverPresentationControllerDelegate to view popovers in iPhones. By default it will be presented in the style of an already presented view controller.
Add this piece of code to present the controller as popover
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(nonnull UITraitCollection *)traitCollection {
return UIModalPresentationNone;
}

presentViewController shows black sceen while UIModalTransitionStyleFlipHorizontal objective c

I'm facing issue with the UIModalTransitionStyleFlipHorizontal, Which gives black screen at background while transition and shows the actual screen.
After first time it shows the last view which added as rootview.
What actually I need to achieve is I don want to show the black and previous screen. It should show an empty screen background as like the below image.
The code I used to achieve the transition is
- (void)popViewController {
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"MainViewController"];
rootController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:rootController];
[self.window.rootViewController presentViewController:navigation animated:YES completion:nil];
[self.window makeKeyAndVisible];
}
I'm using storyboard with dynamic rootview.Anybody help me to fix it out.
Thanks
Remove subview
Add this code on your method popViewController
AppDelegate *appDelegate =(AppDelegate *)[[UIApplication sharedApplication]delegate];
NSArray *subViewArray = [appDelegate.window subviews];
for (id obj in subViewArray)
{
[obj removeFromSuperview];
}
I solved the issue by add window subview with background.
UIImageView* background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
background.image = [UIImage imageNamed:#"TermsBg"];
[self.window addSubview:background];

MBProgressHud with storyboards

The following code is from a sample demo.
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading";
[HUD showWhileExecuting:#selector(myTask) onTarget:self withObject:nil animated:YES];
It uses .xibs as it views. I'm creating an application that uses storyboards. Thus there is no navigationController for the initWithView method when instantiating HUD. Is there any way to implement a HUD without using .xibs and a navigationController. I've tried passing both "self" and "self.view" but they don't work. The class that this is in is the ViewController.m class and it is a UIViewController class. So I don't see why passing self wouldn't work.
This is my code
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
Again "self" is my main ViewController
Thanks!
Here's what I think you want:
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
[view addSubview:hud];
[hud showWhileExecuting:#selector(YOUR_TASK) onTarget:YOUR_TARGET withObject:nil animated:YES]; // or NO if you don't want it to be animated
Alternatively, if you want to manage showing and displaying the HUD manually yourself, there's some nice convenience methods for doing that:
// To add HUD to a view
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; // or NO if you don't want it to be animated
// To set HUD text
[hud setLabelText:#"Text"];
// To remove HUD from a view (such as later in the code, after load, etc)
[MBProgressHUD hideHUDForView:view animated:YES];
Where view is the view you want the HUD added/removed from.
I.e. self.view on a view controller.

View Controller Animation in Xcode

I need to animate a view when user click on the button. i am using the following code for animating
App Delegate
ViewController2 *vc2_controller = [[ViewController2 alloc]init];
UINavigationController *baseViewController = [[UINavigationController alloc]initWithRootViewController:vc2_controller];
[self.window addSubview:baseViewController.view];
self.window.rootViewController = vc2_controller;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
self.viewController = [[vc1controller alloc]initWithNibName:nil bundle:nil];
//ViewController1.m -- This is Working
[self.navigationController pushSlideViewController:self.navigationController];
I created function definition inside the category
//Category
- (void)pushSlideViewController:(UIViewController *)viewController
{
// NSLog(#"In Navigation Controller");
//[UIView transitionWithView:viewController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
CGRect screensize = [[UIScreen mainScreen]bounds];
[UIView animateWithDuration:0.5 animations:^{
NSLog(#"In View ANimatin");
CGRect current_rect = viewController.view.frame;
viewController.view.frame = current_rect;
current_rect.origin.x = screensize.size.width-50;
viewController.view.frame = current_rect;
}];
}
Now i am calling ViewController1.m from ViewController2.m -- Animation is Not Working
ViewController1 *view_control = [[ViewController1 alloc]init];
[self.navigationController popSlideViewController:view_control];
//Category
-(void)popSlideViewController:(UIViewController *)viewController
{
CGRect screensize = [[UIScreen mainScreen]bounds];
//NSLog(#"In Pop");
[UIView animateWithDuration:0.5 animations:^{
//View is not coming to original position.
CGRect current_rect = viewController.view.frame;
NSLog(#"In View ANimatin %f",current_rect.origin.x);
viewController.view.frame = current_rect;
current_rect.origin.x = 0.0;
viewController.view.frame = current_rect;
}];
}
Note: i am not using ios5 :-)
Thanks,
You're loading VC2 from VC1, which, as you say, works fine. Then you're trying to load VC1 from VC2, which is where the problem lies - VC1 is still loaded, behind VC2, so you just need to dismiss VC2 in a similar way to how you brought it up in the first place, just animate it back off the screen, remove it from the view, then release it (unless you released it when you showed it). Alternatively you could use the built-in methods,
presentViewController:animated:completion:
and
dismissViewControllerAnimated:completion:
which have their own animation.

Why is my UInavigationController right button disappearing after it is loaded

I'm using the following code to push a view controller when user click on a UIButton
- (IBAction)showListPicker:(id)sender {
if([audioPlayer isPlaying])
{
[audioPlayer stop];
}
ListPicker *lp = [[ListPicker alloc] initWithStyle:UITableViewStyleGrouped];
[[self navigationController] pushViewController:lp animated:YES];
[lp release];
}
In the ViewDidLoad of ListPicker I use the following code to add right navigational control button
-(void)viewDidLoad{
[[self navigationController] setNavigationBarHidden:NO];
[[[self navigationController] navigationBar] setTintColor:[UIColor brownColor]];
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(doSomething:)];
[[self navigationItem] setRightBarButtonItem:button];
[button release];
}
When the view is loaded, I can see the right button briefly but then it disappear right away. What am I doing wrong?
Thank you.
My guess is that your ListPicker's XIB contains another navigation bar, obstructing the actual navigation controller's bar. This is why you see it for an instant and then it "disappears". If it's not something in the XIB, check the code all the way from instantiating that new view controller until the viewDidAppear of ListPicker.