iPhone modal View with parent view semi-visible?
I am adding a modal view using the following code:
[self presentModalViewController:phrasesEditor animated:YES];
How can I make the modal view semi-transparent so that the superview "shines" through?
My complete method/function looks like this:
-(IBAction)showEditPhrases:(id)sender{
PhrasesViewController *phrasesEditor = [[PhrasesViewController alloc] initWithNibName:@"PhrasesViewController" bundle:nil];
phrasesEditor.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[phrasesEditor.view setAlpha: 0.5];
[phrasesEditor.view setBackgroundColor: [UIColor clearColor]];
[self presentModalViewController:phrasesEditor animated:YES];
[phrasesEditor release];
}
EDIT:
The pragmatic approach of changing the alpha will no开发者_C百科t work, apparently. How can I load a NIB into a UIView and interact with it?
I have a UIViewController now. Do I convert/modify/change it into a UIVIew and then load it, or am I supposed to do something else?
EDIT 2:
I tried [self.view addSubview:phrasesEditor.view];
, but that leaves me with no way to remove the subview. Each view seems to have its own View Controller.
EDIT 3:
I thought that I should mention that the superview is inside of a view controller called iDecideViewController and the phrasesEditor has a separate View Controller.
I Had the same problem and added a property and 2 methods to a subclass of UIViewController to mimic the behavior of presentModalViewController:animated:
For mor information, see my blog post: How to display a transparent modal view controller or check out the code below:
#pragma mark - Transparent Modal View
-(void) presentTransparentModalViewController: (UIViewController *) aViewController
animated: (BOOL) isAnimated
withAlpha: (CGFloat) anAlpha{
self.transparentModalViewController = aViewController;
UIView *view = aViewController.view;
view.opaque = NO;
view.alpha = anAlpha;
[view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *each = obj;
each.opaque = NO;
each.alpha = anAlpha;
}];
if (isAnimated) {
//Animated
CGRect mainrect = [[UIScreen mainScreen] bounds];
CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
[self.view addSubview:view];
view.frame = newRect;
[UIView animateWithDuration:0.8
animations:^{
view.frame = mainrect;
} completion:^(BOOL finished) {
//nop
}];
}else{
view.frame = [[UIScreen mainScreen] bounds];
[self.view addSubview:view];
}
}
-(void) dismissTransparentModalViewControllerAnimated:(BOOL) animated{
if (animated) {
CGRect mainrect = [[UIScreen mainScreen] bounds];
CGRect newRect = CGRectMake(0, mainrect.size.height, mainrect.size.width, mainrect.size.height);
[UIView animateWithDuration:0.8
animations:^{
self.transparentModalViewController.view.frame = newRect;
} completion:^(BOOL finished) {
[self.transparentModalViewController.view removeFromSuperview];
self.transparentModalViewController = nil;
}];
}
}
When you present a modal view it will actually unload the previous view so that is not what you want at all. UIView has a method called addSubview which will put the new view on top of its siblings.
Instead of:
[self presentModalViewController:phrasesEditor animated:YES];
do:
[self.view addSubview:phrasesEditor.view];
This is assuming you are already in a ViewController subclass.
精彩评论