I want to show UIView in UIAlert and alert should be display in all another view and also when application running in background. Is it Possible ? if No then how can i do it?
Try This Code :-
In view didload Method :
-(void)viewDidLoad
{
flag=YES;
[super viewDidLoad];
}
In viewWillAppear Method :
-(void)viewWillAppear:(BOOL)animated
{
v=[[UIView alloc]initWithFrame:CGRectMake(0, 490, 320, 300)];
v1.layer.cornerRadius = 10.0;
v1.clipsToBounds = YES;
v1.frame=CGRectMake(0, 490, 320, 108);
}
And
-(IBAction)btn_Clicked
{
if (flag == NO)
{
v=[[UIView alloc]initWithFrame:CGRectMake(0, 490, 320, 300)];
v1.layer.cornerRadius = 10.0;
v1.clipsToBounds = YES;
v1.frame=CGRectMake(0, 490, 320, 108);
v.hidden=NO;
v.frame=CGRectMake(0, 0, 320, 460);
v.backgroundColor=[UIColor colorWithWhite:0 alpha:0.2];
[self.view addSubview:v];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
v1.frame=CGRectMake(69, 176, 185, 108);
[v addSubview:v1];
[UIView commitAnimations];
}
else
{
v.hidden=NO;
v.frame=CGRectMake(0, 0, 320, 460);
v.backgroundColor=[UIColor colorWithWhite:0 alpha:0.2];
[self.view addSubview:v];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
//v1.backgroundColor=[UIColor colorWithWhite:0 alpha:0.3];
v1.frame=CGRectMake(69, 176, 185, 108);
[v addSubview:v1];
[UIView commitAnimations];
}
}
And
-(IBAction)cancel_Clicked
{
flag=NO;
v.hidden=YES;
}
When your app goes in background you dont have that much access in showing UIAlertView, you can try UILocalNotification.
But I guess you want a custom popup which is your own view.
just try creating a UIView and set properties from Interface Builder. and add as a subview.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 290, 290)];
[self.view addSubView:v];
but once your app is in background, you can't do anything anymore.
You can add a UIView in UIAlertView,
but application running in background you can't, it 's only the notification style.
Related
I am trying to add a subview (UITextField) to a view and change the frame property of a UITextView in the same method but it's not working as I was expecting. This is the code:
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
textField.frame = CGRectMake(0, 120, 240, 40);
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
[UIView commitAnimations];
[self.view insertSubview:textField aboveSubview:self.tvContent];
Note: tvContent is the UITextView object which I am trying to reposition.
The problem I am facing is that the UITextView isn't moving at all IF the last line of code (insertSubview) is in place. The UITextField does show up though. However, if I remove the last line , the textview change its position just fine.
I have also tried without animation by just setting the frame property of self.tvContent and same behavior occurred.
Any ideas to fix this?
Do like this,
UITextField *textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
textField.frame = CGRectMake(0, 120, 240, 40);
[UIView beginAnimations:#"MoveView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
[UIView setAnimationDidStopSelector:#selector(animationFinish)];
[UIView commitAnimations];
-(void)animationFinish {
[self.view insertSubview:textField aboveSubview:self.tvContent];
}
Is there a reason you are still using the good ol' beginAnimations thing?
This would do the same:
[UIView animateWithDuration:0.5 animations:^(void) {
self.tvContent.frame = CGRectMake(0, 80, self.tvContent.frame.size.width, self.tvContent.frame.size.height);
} completion:^(BOOL finished) {
[self.view addSubview:textField];
}];
EDIT: What happens if you just addSubView it?
I'm coding an iPhone TabBar Project with UiNavigation, the first view contain UIScrollView and the scrollview contain views every subview contain imageview,anUiImage and a UiButton follows
is the function that build the first screen
//*******************************************************
//* Build the main screen *
//*******************************************************
-(void) buildScreen{
sing.viewsArray = [[NSMutableArray alloc]init];
int Vve = 10;
int Vho = 10;
int wi = 95;
int hi = 95;
int ArrayIndex = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
staticView = [[UIView alloc] initWithFrame:CGRectMake(Vve, Vho, 100, 100)];
[staticView setTag:ArrayIndex];
staticImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[sing.stdPicArray objectAtIndex:ArrayIndex]]];
[staticImage setFrame:CGRectMake(0, 0, wi, hi)];
[staticView addSubview:staticImage];
viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
viewButton.frame = CGRectMake(0, 0, wi, hi);
[viewButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[viewButton setTag:ArrayIndex];
[sing.buttonsArray addObject:viewButton];
[staticView addSubview:viewButton];
[sing.viewsArray addObject:staticView];
[MainScroll addSubview:[sing.viewsArray objectAtIndex:ArrayIndex]];
Vve += 100;
ArrayIndex++;
}
Vve = 10;
Vho += 100;
}
}
When I click on the UiButton I want to add another UIView to the UIScrollView and make a "UIViewAnimationTransitionFlipFromLeft" when the view is added to the scroll view
tried the following code
-(void)animateFlip{
[UIView transitionWithView:flipViewBack
duration:1.0
options:UIViewAnimationTransitionFlipFromLeft
animations:^{ [self.view addSubview:flipViewBack]; }
completion:^(BOOL f){ }];
}
didn't work just added the flipViewBack
-(void)animateFlip{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.flipViewFront
cache:YES];
[MainScroll addSubview:flipViewFront];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
didn't work just like the other one, the next code did flip the main view with the view I wanted, but it didn't flip only the the view I want
-(void)animateFlip{
[UIView beginAnimations:nil
context:nil];
[UIView setAnimationDuration:1.0];
[UIView transitionFromView:self.view
toView:flipViewBack
duration:2
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:NULL];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
//set transformation
[UIView commitAnimations];
}
Does any body know how to do it' I looked everywhere and all the code I found didn't work for me
Thanks
I tried another option suggested in this link
iPhone subview flip between 2 views and it didn't work for me
this is the function that I use on click in the last 3 lines im adding to the flipView (container) the flipViewFront view then I add the flipView to the mainScroll view and then I go to execute the animation in the animateFlip that I copied from your sample
//*******************************************************
//* Click on the picture as button *
//*******************************************************
-(void) buttonClick:(id) sender{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
NSLog(#"Button pressed tag: %i",row);
self.flipView = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipView.backgroundColor = [UIColor clearColor];
self.flipViewBack = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront.backgroundColor = [UIColor whiteColor];
self.flipViewImage = [UIImage imageNamed:[sing.stdPicArray objectAtIndex:row]];
self.filpImage = [[UIImageView alloc]initWithImage:self.flipViewImage];
[self.flipViewBack addSubview:self.filpImage];
self.flipCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.flipCloseButton setFrame:CGRectMake(0, 0, 24, 24)];
[self.flipCloseButton setImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
[self.flipCloseButton addTarget:self action:#selector(closwFlip:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipCloseButton];
self.flipOkButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, 190, 190)];
[self.flipCloseButton addTarget:self action:#selector(continueTo:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipOkButton];
[flipView addSubview:flipViewFront];
[MainScroll addSubview:flipView];
[self animateFlip];
}
found the problem, but not the answer, if it works directly from the ViewDidLoad it and the first view is on screen all is well and the flip works' if I change it so that the want to add a view and flip it in one event it shows me the second(flipped) view without the flip
Your problems seems to be that you are trying to animate the flip on the view before it is added to the view hierarchy.
Instead you should add the back view to a common container view (that later will contain the front view) and then flip the container view while adding the front and removing the back, like this:
// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview]; // This is already added to the container
[containerView addSubview:frontView]; // Not yet added ...
}
completion:NULL];
Update
This is all the code I wrote when answering this question. It works for me. If it still doesn't animate for you then you probably haven\t set up your view hierarchy the same way.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:containerView];
frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[frontView setBackgroundColor:[UIColor orangeColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:backView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(flip)];
[self.view addGestureRecognizer:tap];
}
- (void)flip {
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview];
[containerView addSubview:frontView];
}
completion:NULL];
}
I have a small code to display image 1 and after 2 seconds replace image1 by image2 with animation below
UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 410, 1020, 400)];
UIImage *image = [UIImage imageNamed:#"img.jpeg"];
view1.image = image;
[self.view addSubview:view1];
UIImageView *view2 = [[UIImageView alloc] init ];
view2.frame = CGRectMake(0, 410, 0, 400);
view2.image = [UIImage imageNamed:#"bien.jpeg"];
[self.view addSubview:view2];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelay:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeView: view1:)];
view2.frame = CGRectMake(0, 410, 800, 400);
[UIView commitAnimations];
and function removeView to remove view1 from superview below:
-(void)removeView: (UIImageView *)view1{
[view1 removeFromSuperview];
}
So i dont know why my function to remove view1 from superview not work, please help me! Thanks alot...
The selector cannot pass the parameter. Modify your method to
-(void)removeView{
[view1 removeFromSuperview];
}
where "view1" is a instance to your view.
and your selector to:
[UIView setAnimationDidStopSelector:#selector(removeView)];
I would recommend you to use block based animations (available since iOS 4).
They are much easier to use and don't need to send parameters through methods and all that stuff.
Example:
UIImageView *view1 = [[UIImageView alloc] init];
//initialize your UIImageView view1
[self.view addSubview:view1];
UIImageView *view2 = [[UIImageView alloc] init];
//initialize your UIImageView view2
[UIView animateWithDuration:2 animations:^{
//here happens the animation
[self addSubview:view2];
} completion:^(BOOL finished) {
//here happens stuff when animation is complete
[view1 removeFromSuperView];
}];
Remember to vote up and or mark as accepted answer ;)
Try this code!!
UIImageView *view1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 410, 1020, 400)];
UIImage *image = [UIImage imageNamed:#"img.jpeg"];
view1.tag = 1;
view1.image = image;
[self.view addSubview:view1];
UIImageView *view2 = [[UIImageView alloc] init ];
view2.frame = CGRectMake(0, 410, 0, 400);
view2.image = [UIImage imageNamed:#"bien.jpeg"];
[self.view addSubview:view2];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationDelay:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeView: view1:)];
view2.frame = CGRectMake(0, 410, 800, 400);
[UIView commitAnimations];
// Remove View Method
-(void)removeView : (UIImageView *) imgVew {
if (imgVew.tag == 1)
[imgVew removeFromSuperView];
}
Access it -
[UIView setAnimationDidStopSelector:#selector(removeView:)];
the problem is simple, this selector is not exists in your class: -removeView:view1:. thus there is nothing to call back after the animation finished. this is why your -(void)removeView:(UIImageView *)view1; method will be never called back.
please, realised your real selector is -removeView: and it is not equal with -removeView:view1:
if you want to pass parameter through the didStopSelector, I would have a bad news: you cannot do it as you did in your code, so this part is wrong at all:
// WRONG AT ALL:
[UIView setAnimationDidStopSelector:#selector(removeView:view1:)];
// PROPER WAY:
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
because the didStopSelector must be the following kind of selector with the following parameters.
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
you can pass parameter for your callback method like this:
[UIView beginAnimations:nil context:view1]; // see the context's value
and in your didStopSelector you can use it somehow like:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[((UIView *)context) removeFromSuperView];
}
Just wondering why and need to solve it. i have this searchbar looks like this.
which clicked will move up to the top and expend to 0,0,320,searchbar's height.
which clicked Cancel, it should resize back to 33,55,270, searchbar's height. but it doesn't
the searchDisplayControllerDidEndSearch will work to resize back, however there is a 1 sec lag that user could see the changes. any body why in searchDisplayControllerWillEndSearch the animation resize wound't work ?.
thanks for the comments and feedback.
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[UIView animateWithDuration:.1
animations:^{
controller.searchResultsTableView.frame = frogTableView.frame;
searchDisplayController.searchResultsTableView.frame = frogTableView.frame;
searchDisplayController.searchBar.frame = CGRectMake(32,
55,
270,
searchDisplayController.searchBar.frame.size.height);
searchBar.frame = CGRectMake(32,
55,
270,
searchBar.frame.size.height);
frogTableView.frame = CGRectMake(frogTableView.frame.origin.x,
121,
frogTableView.frame.size.width,
frogTableView.frame.size.height);
notePad.frame = CGRectMake(notePad.frame.origin.x,
45,
notePad.frame.size.width,
notePad.frame.size.height);
searchDisplayController.searchResultsTableView.frame = frogTableView.frame;
}
completion:^(BOOL finished){
//whatever else you may need to do
}];
}
I had the same trouble after I started to use UISearchBar with UISearchDisplayController. The solution I came up with is to place a search bar on a view:
UIView * searchBarSubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT)] ;
searchBarSubview.autoresizesSubviews = YES;
searchBarSubview.backgroundColor = [UIColor clearColor];
UISearchBar * globalSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT)] autorelease];
globalSearchBar.tintColor = [UIColor blackColor];
globalSearchBar.delegate = self;
[globalSearchBar setBackgroundImage:[UIImage alloc]];
and resize the view instead of search bar in searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
// Slide in to the final destination
searchBarSubview.frame = CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT);
[UIView commitAnimations];
I have a UIScrollView which has a UIView embedded in it. It have 2 buttons "scroll" and "unscroll".On clicking the "scroll" button, my scrollview scroll up from the bottom of the parent view. Everything works fine until here but when I click the "unscroll" button to push the scrollview back to where it came from, nothing happens. I have posted the entire code here. Please check where the fault lies !!. Already spent a sizeable amount of time on it.
-(IBAction)unscrollClicked:(id)sender
{
//[viewController.view removeFromSuperview];
[UIView beginAnimations:#"back to original size" context:nil];
scrollView.contentSize=viewController.view.bounds.size;
//scrollView.contentSize=viewController.view.bounds.size;
[UIView commitAnimations];
}
-(IBAction)scrollClicked:(id)sender
{
//viewController.view.backgroundColor=[UIColor orangeColor];
viewController.view.frame=CGRectMake(0, 410, 320, 460);
//[self.view addSubview:viewController.view];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,40, 320, 400)];//(0,0,320,160)
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//[MyView setFrame:CGRectMake (0, 0, 320, 170)];
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollView.contentSize = viewController.view.bounds.size;
scrollView.bounces = YES;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 5.0;
scrollView.delegate = self;
[scrollView addSubview: viewController.view];
[self.view addSubview:scrollView];
[self moveScrollView:scrollView];
}
-(void) moveScrollView:(UIScrollView *)scrollView
{
CGFloat scrollamount=400;
[scrollView setContentOffset:CGPointMake(0,scrollamount) animated:YES];
}
1st problem i see with this code is that you initialize scrollView in scrollClicked and never store pointer to it, so in unscrollClicked pointer should be nil.
Another problem is that as far as i know, animation is conducted this way:
[UIView beginAnimations:#"Animate" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationBeginsFromCurrentState:YES];
[somView setFrame:CGRectMake(100, 100, 100, 100)];
[UIView commitAnimations];