I want to create a custom alert view within my iOS application. For example, I want to put some images in this alert, and change its color.
I know how to create a normal UIAlertView, but is there a way to customize an alert view?
I set up my own UIViewController which I can skin with my own images. I generally only use one or two buttons, so I hide the second button if it's not being used. The view is actually the size of the entire screen, so it blocks touches behind it, but it is mostly transparent, so the background shows through.
When bringing it in, I use a few animations to make it bounce like Apple's alert view. Something like this works:
-(void)initialDelayEnded {
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
self.view.alpha = 1.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce1AnimationStopped)];
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounce1AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounce2AnimationStopped)];
self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounce2AnimationStopped {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/2];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
I have the possibility of a short delay built into the class, so initialDelayEnded is called when that delay is over.
When initializing, I pass in an object and selector I want called when each button is pressed, and then I call the appropriate selector on the object when a button is pressed.
Here's a custom alert view that I wrote which is a drop-in replacement for UIAlertView. You can set a custom background image; it wouldn't be hard to extend to support custom background colors.
https://github.com/TomSwift/TSAlertView
Create one sub class for UIAlertView.
And create common class for Alert View method.
Add 2 below method for it.
#pragma mark Alert View Functions
+(void)alertViewWithYesNo:(NSString *)pstrTitle:(NSString *)pstrMessage:(int)pstrTagId:(id)pDelegate{
UIAlertView *objAlertNotify = [[UIAlertView alloc] init];
[objAlertNotify setDelegate:pDelegate];
[objAlertNotify addButtonWithTitle:#""];
[objAlertNotify addButtonWithTitle:#""];
int intTemp = 1;
for (UIView* view in [objAlertNotify subviews])
{
if ([[[view class] description] isEqualToString:#"UIAlertButton"])
{
UILabel *theTitle = [[UILabel alloc] init];
[theTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:g_AlertFontSize]];
[theTitle setTextColor:[UIColor whiteColor]];
switch (intTemp) {
case 1:
[theTitle setText:#"Yes"];
//[theTitle setTextColor:g_ColorYes];
break;
case 2:
[theTitle setText:#"No"];
//[theTitle setTextColor:g_ColorNo];
break;
}
intTemp++;
[theTitle setBackgroundColor:[UIColor clearColor]];
[theTitle setTextAlignment:UITextAlignmentCenter];
[view addSubview:theTitle];
}
else if ([[[view class] description] isEqualToString:#"UIThreePartButton"])
{
UILabel *theTitle = [[UILabel alloc] init];
[theTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:g_AlertFontSize]];
[theTitle setTextColor:[UIColor whiteColor]];
switch (intTemp) {
case 1:
[theTitle setText:#"Yes"];
//[theTitle setTextColor:g_ColorYes];
break;
case 2:
[theTitle setText:#"No"];
//[theTitle setTextColor:g_ColorNo];
break;
}
intTemp++;
[theTitle setBackgroundColor:[UIColor clearColor]];
[theTitle setTextAlignment:UITextAlignmentCenter];
[view addSubview:theTitle];
}
}
[objAlertNotify setTag:pstrTagId];
[objAlertNotify setTitle:pstrTitle];
[objAlertNotify setMessage:pstrMessage];
[objAlertNotify show];
}
+(void)alertViewBtnText:(UIAlertView *)alertView{
for (UIView* view in [alertView subviews])
{
//NSLog(#"%#", [[view class] description]);
if ([[[view class] description] isEqualToString:#"UIAlertButton"])
{
for (UILabel *lbl in [view subviews])
{
//NSLog(#"%#", [[lbl class] description]);
if ([[[lbl class] description] isEqualToString:#"UILabel"])
{
CGRect frame = [view bounds];
CGSize maximumLabelSize = CGSizeMake(320,480);
CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode];
CGRect newFrame = lbl.frame;
newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
lbl.frame = newFrame;
//frame.size.width = 320.0;
//frame.size.height = 480.0;
lbl.frame = frame;
[lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];
}
}
}
else if ([[[view class] description] isEqualToString:#"UIThreePartButton"])
{
for (UILabel *lbl in [view subviews])
{
CGRect frame = [view bounds];
CGSize maximumLabelSize = CGSizeMake(320,480);
CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode];
CGRect newFrame = lbl.frame;
newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
lbl.frame = newFrame;
//frame.size.width = 320.0;
//frame.size.height = 480.0;
lbl.frame = frame;
[lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)];
}
}
}
}
Now, in whatever class, you are using this custom Alert:
Add below:
#pragma mark UIAlertViewDelegate
-(void)willPresentAlertView:(UIAlertView *)alertView{
if(alertView==objAlertMsg){
/*clsCommonFuncDBAdapter *objclsCommonFuncDBAdapter = [[clsCommonFuncDBAdapter alloc] init];
float newHeight = [objclsCommonFuncDBAdapter getAlertHeightByMessage:alertView.frame.size.width :alertView.message] + [g_AlertExtraHeight intValue];
[objclsCommonFuncDBAdapter release];
//NSLog(#"X = %f, Y = %f, Widht = %f, Height = %f", alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, alertView.frame.size.height);
//[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, 110.0)];
[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, newHeight)];*/
}
[clsCommonFuncDBAdapter alertViewBtnText:alertView];
}
For calling it:
Use like below:
-(void)askForGPSEnable{
[clsCommonFuncDBAdapter alertViewWithYesNo:msgGPSTitle :msgGPSMessage :0 :self];
}
Let me know in case of any difficulty.
You'll need to create your own custom view, and implement some alert view style behaviour, such as displaying modally, dimming the background, animating in and out, etc.
There's no support in the SDK for customising a UIAlertView any further than the text or buttons.
http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
and
http://www.skylarcantu.com/blog/2009/08/14/custom-uialertview-color-chooser/
You can use above links for custom alerts. I hope these will be helpful for you.
There is very good example on Git hub for custom alert view. It is using UI alert view in core and provides number of methods to customize alert view in different ways
Related
Okay I'm lost here.
So have this UIViewController we can call it viewController1 where I add a new editable UITextView for every click on an add button.
I then use UIPanGestureRecognizer so you can drag them where ever you want. This works just fine. Now I want to go to viewController2: [self.navigationController pushViewController:viewController2 animated:YES]; but when I press the back button to return to viewController1 the first UITextView that was created in viewController1 has changed. It's not gone but the text property inside the textview has moved.
This just happens for the first instance of UITextView that I create, any ideas here would be appreciated.
Here's my method for creating the text:
-(void)addTextFieldToScreen
{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 140, 120)];
textView.text = #"TEXT";
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor purpleColor];
textView.font = [UIFont fontWithName:#"AmericanTypewriter" size:30];
textView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveText:)];
panRecognizer.minimumNumberOfTouches = 1;
panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.delegate = self;
[textView addGestureRecognizer:panRecognizer];
[self.view addSubview:textView];
}
And here's my method for making my textView dragable:
-(void)moveText:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat workingWidth = screenRect.size.width - 40;
CGFloat workingHeight = screenRect.size.height - 80;
if(finalX < 50) {
finalX = 50;
}
else if(finalX > workingWidth ) {
finalX = workingWidth;
}
if(finalY < 80) {
finalY = 80;
}
else if(finalY > workingHeight) {
finalY = workingHeight;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:.20];
[UIView commitAnimations];
}
}
EDIT:
Okay here's whats going on, I add two textviews with content "TEXT" with the "add text" button in the right corner in viewController1:
And then when I come back from viewController2 to viewController1 this is what I see:
As you can see it's only the left textView instance text property that change position, and this is the first one I'm adding.
I am doing an app in which i need to draw lines in one view and that automatically should appear in the other view. Any help would be appreciated..
I have tried this..
- (void)viewDidLoad
{
[super viewDidLoad];
slv = [[SmoothLineView alloc] initWithFrame:CGRectMake(DrawingView.bounds.origin.x, DrawingView.bounds.origin.y + 42, DrawingView.bounds.size.width, DrawingView.bounds.size.height - 42)];
slv.delegate = self;
[DrawingView addSubview:slv];
}
-(IBAction)btnAnotherView:(UIButton *)sender
{
[zoomingTypingView setUserInteractionEnabled:YES];
if(sender.tag == 123)
{
[self.view setBackgroundColor:[UIColor colorWithWhite:0.500 alpha:1.000]];
[UIView animateWithDuration:0.5
delay:0.1
options:UIViewAnimationTransitionCurlUp
animations:^{
//animation code
zoomingTypingView.frame = CGRectMake(0, 549, 768, 451);
} completion:^(BOOL finished) {
}];
CGRect imageFrame = CGRectMake(50, 50, 220, 120);
ResizableView = [[SPUserResizableView alloc] initWithFrame:imageFrame];
CGRect gripFrame = CGRectMake(50,50, 220,120);
UIView *zoom = [[UIView alloc]initWithFrame:gripFrame];
UIScrollView *zoomscroll = [[UIScrollView alloc]initWithFrame:gripFrame];
[zoomscroll setZoomScale:32.0];
[zoomscroll setMaximumZoomScale:32.0];
[zoom addSubview:zoomscroll];
[ResizableView setContentView:zoom];
ResizableView.delegate = self;
[DrawingView addSubview:ResizableView];
sender.tag = 246;
}
else
{
[UIView animateWithDuration:0.5
delay:0.1
options:UIViewAnimationTransitionCurlUp
animations:^{
//animation code
zoomingTypingView.frame = CGRectMake(0, 999, 768, 451);
} completion:^(BOOL finished) {
[self.view setBackgroundColor:[UIColor whiteColor]];
}];
[ResizableView removeFromSuperview];
[DrawingView addSubview:slv1];
sender.tag = 123;
}
}
In the above Image WhiteColored View is the new View and if i draw in that whiteColoredView that should reflect in both the views.
You can get more idea by looking at the NOTE TAKER HD APP.
http://www.youtube.com/watch?v=FdGDnUKZcMM
If you have two views defined:
#property(nonatomic) UIView *view1;
#property(nonatomic) UIView *view2;
instead of do self.view, you can have function does the drawing for one view:
-(void)drawView:(UIView*)view{
//your drawing code here
}
And just call the same function twice with passing different views.
I am trying to display a UIImage in a UIAlert.
I want to display it nearly fullscreen. For example my image has a 300x450px size.
Well I add it as a subview to my UIAlert.
But the UIAlert has default coordinates to keep it centered.
So I can add a image bigger than the UIAlert frame, but it covers the UIAlert...
I tried to specify a frame for my UIAlert, but it has no effect on it.
In fact I would like to add my image as a real content of the UIAlert, as well as simple text.
Is this possible ?
Consider writing your own Dialog popup instead of messing with the UIAlertView. If you want the 'bounce-in' animation that can easily be achieved using transform animations. Here is a simple popup dialog box I've just made.
To test it out create a new View-Based Application project and add this class. Then you can test it by adding the 'usage' code below to your *ViewController.m
This is a very simple example to demo the theory. It would make more sense to have a static method in SimpleDialog that shows the popup, but I didn't want to make the example overly complex.
SimpleDialog.h
#import <UIKit/UIKit.h>
#interface SimpleDialog : UIView
{
}
- (void) show;
#end
SimpleDialog.m
#import "SimpleDialog.h"
#define BOUNCE_SPEED 1
#implementation SimpleDialog
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
self.backgroundColor = [UIColor greenColor];
UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(0, 0, 200, 20);
closeButton.center = self.center;
[closeButton setTitle:#"Close" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
}
return self;
}
- (void) show
{
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:self];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounceInStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounceInStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounceOutStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounceOutStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
self.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
- (void) hide
{
[self removeFromSuperview];
}
#end
Usage
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* popButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
popButton.frame = CGRectMake(0, 0, 200, 20);
popButton.center = self.view.center;
[popButton setTitle:#"Pop" forState:UIControlStateNormal];
[popButton addTarget:self action:#selector(showIt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:popButton];
}
- (void) showIt
{
SimpleDialog* simple = [[SimpleDialog alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
simple.center = self.view.center;
[self.view addSubview:simple];
[simple show];
[simple release];
}
Yes this is possible.
But it requires to customize the UIAlertView.
Using UIAlertView customization you can add anything.
i have a simple app, it consist of 2 textview, 1 uiview as a coretext subclass, and then 1 scrollview. the others part is subviews from scrollview. I use this scrollview because i need to scroll the textviews and uiview at the same time. I already scroll all of them together, but the problem is, the keyboard hiding some lines in the textview. I have to change the frame of scrollview when keyboard appear, but it still not help.
This is my code :
UIScrollView *scrollView;
UIView *viewTextView;
UITextView *lineNumberTextView;
UITextView *codeTextView;
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
self.scrollView.frame = CGRectMake(0, 88, self.codeTextView.frame.size.width,
self.codeTextView.frame.size.height);
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, viewTextView.frame.size.height);
[scrollView addSubview:viewTextView];
CGAffineTransform translationCoreText = CGAffineTransformMakeTranslation(60, 7);
[viewTextView setTransform:translationCoreText];
[scrollView addSubview:lineNumberTextView];
[self.scrollView setScrollEnabled:YES];
[self.codeTextView setScrollEnabled:NO];
}
-(void)keyboardWillAppear:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[[[notification userInfo]
objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
CGRect keyboardEndingUncorrectedFrame = [[[notification userInfo]
objectForKey:UIKeyboardFrameEndUserInfoKey ] CGRectValue];
CGRect keyboardEndingFrame =
[self.view convertRect:keyboardEndingUncorrectedFrame
fromView:nil];
self.scrollView.frame = CGRectMake(0, 88, self.codeTextView.frame.size.width,
self.codeTextView.frame.size.height - keyboardEndingFrame.size.height);
[UIView commitAnimations];
}
-(void)keyboardWillDisappear:(NSNotification *) notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[[[notification userInfo]
objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
CGRect keyboardEndingUncorrectedFrame = [[[notification userInfo]
objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardEndingFrame =
[self.view convertRect:keyboardEndingUncorrectedFrame
fromView:nil];
self.scrollView.frame = CGRectMake(0, 88, self.codeTextView.frame.size.width,
self.codeTextView.frame.size.height + keyboardEndingFrame.size.height);
[UIView commitAnimations];
}
can somebody help me please?
UPDATE
this is a pic from this problem :
some text still hiding by the keyboard after i do my code
UPDATE AGAIN
i think the keyboard still hiding the text because i set the textview scroll enable to be NO. is that right??
i have add this code to the keyboardwillappear method
codeBuilderSelectedRange = self.codeTextView.selectedRange;
[self.viewTextViewScroll setContentOffset:CGPointMake(0, (CGFloat)codeBuilderSelectedRange.location) animated:YES];
but it just make the textview dissappear from the view...can somebody tell the answer?
#pragma mark -------------------------
#pragma mark TextView delegate Methods
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 60, 32)];
[infoButton setBackgroundImage:[UIImage imageNamed: #"back-btn.png"] forState:UIControlStateNormal];
[infoButton setTitle:#"Done" forState:UIControlStateNormal];
infoButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];
[infoButton addTarget:self action:#selector(resignTextView) forControlEvents:UIControlEventTouchUpInside];
[parentView addSubview:infoButton];
[infoButton release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
CGRect frame = self.scrollView.frame;
self.scrollView.frame.size.height = 206;
//[self.view setContentOffset:CGPointMake(0,320)];
}
- (void)resignTextView {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
CGRect frame = self.scrollView.frame;
self.scrollView.frame.size.height = 460;
[messageTextView resignFirstResponder];
self.navigationItem.rightBarButtonItem = nil;
}
Here is some code that I have used. Basically, what we're going to do is animate the position of the view whenever a UITextField gets focus. To do this, we must make our UIViewController a delegate of our UITextFields
The first delegate method you need to implement is - (void)textFieldDidBeginEditing:(UITextField *)textField
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds
fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds
fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5
* textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
Now that the view has been 'pushed up' when a UITextField is selected, we want to make sure that we slide it back down when we're done:
- (void) textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
This was taken from one of my favorite Cocoa blogs, Cocoa With Love. Cheezy name, but Matt's got some great posts on Objective-C development.
Specifically for UITextView (not UITextField)
you may have a look at the following tutorial: http://codingcluster.blogspot.in/2012/03/iphone-make-uitextview-move-up-when.html
i have implemented it and it is working as expected(at least for me)
I like to replicate the form behavior of Safari on the iPhone in my own app. If you enter data in an web form you get a separate UIToolbar (previous, next, done) just above the UIKeyboardView. Same for choosing an option: you get the same UIToolbar just above an UIPickerView.
I am looking for demos / sourcode / ideas how to implement this. Would I create my own subview with that toolbar and textview / pickerview? Is there a more elegant way? Especially something that leverages becomeFirstResponder of UITextfield?
So i created a UIViewCOntroller subclass to manage this.
on that i wrote this function to add.
-(void) addToViewWithAnimation:(UIView *) theView
{
UIView* myview = self.view;
CGRect frame = myview.frame;
frame.origin.y = 420;
myview.frame = frame;
UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
bgView.backgroundColor = [UIColor blackColor];
bgView.alpha = 0.6;
backgroundView = bgView;
[theView addSubview: bgView]; // this adds in the dark background
[theView addSubview:self.view]; // this adds in the pickerView with toolbar.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
frame = myview.frame;
frame.origin.y = 420 - frame.size.height;
myview.frame = frame;
[UIView commitAnimations];
}
I then created the view in IB, here is what my class Header looked like at the end of that. (there is also a UItoolbar on the view i just do not have a reference to it in my Controller)
#interface PropertyPickerController : UIViewController {
IBOutlet UIPickerView* Picker;
IBOutlet UIButton* DoneButton;
IBOutlet UIButton* CancelButton;
UIView* backgroundView;
NSArray* SimpleObjects;
id PickerObjectDelegate;
SEL PickerObjectSelector;
}
To then hide the view i use.
-(void) removeFromSuperviewWithAnimation
{
UIView* myview = self.view;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(AnimationDidStop:)];
[UIView setAnimationDuration:0.5];
// set fram below window.
CGRect frame = myview.frame;
frame.origin.y = 420;
myview.frame = frame;
backgroundView.alpha = 0; //fades shade to nothing
[UIView commitAnimations];
}
-(void) AnimationDidStop:(id) object
{
[self.view removeFromSuperview]; //removes view after animations.
[backgroundView removeFromSuperview];
}
And last but not least all the delegate functions for the picker.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
FBSimpleObject* object = (FBSimpleObject*)[SimpleObjects objectAtIndex:row];
return object.Name;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{ return 1;}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [SimpleObjects count];
}
- (IBAction)CancelButtonClick
{
[self removeFromSuperviewWithAnimation];
}
- (IBAction)DoneButtonClick
{
//This performs a selector when the done button is clicked, makes the controller more versatile.
if(PickerObjectDelegate && PickerObjectSelector)
{
NSMethodSignature* signature = [PickerObjectDelegate methodSignatureForSelector:PickerObjectSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:PickerObjectDelegate];
[invocation setSelector:PickerObjectSelector];
[invocation setArgument:&object atIndex:2];
[invocation retainArguments];
[invocation invoke];
}
}
This is how you do the ToolBar. Basically i use the same concept with a ViewController subclass, and i dont use the standard push view or modal display options. (the example here actually places a Textbox and a toolbar on top of the keyboard.
#interface BugEditCommentController : UIViewController {
UITextView* Comment;
UIToolbar* Toolbar;
}
-(void) addToViewWithAnimation:(UIView*) theView;
To activate this view usually you would call [object becomeFirstResponder];
so if you add this to your view Controller constructor, all you need to do is call [object becomeFirstResponder];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:#selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
abd if you implement this method on your controller (defined in the above code)
-(void) keyboardWillShow:(NSNotification *) note
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect toolbarFrame = Toolbar.frame;
CGRect keyboardFrame;
CGPoint keyboardCenter;
[[note.userInfo valueForKey:UIKeyboardCenterEndUserInfoKey] getValue:&keyboardCenter];
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardFrame];
//CGRect toolbarRect = Toolbar.center;
toolbarFrame.origin.y= keyboardCenter.y - ((keyboardFrame.size.height/2) + (toolbarFrame.size.height));
Toolbar.frame = toolbarFrame;
[UIView commitAnimations];
}
-(void) keyboardWillHide:(id) object
{
//you could call [self removeFromSuperviewHere];
}
-(void) removeFromsuperViewWithAnimation
{
[Comment resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(AnimationDidStop:)];
CGRect frame = Toolbar.frame;
frame.origin.y = 480;
Toolbar.frame = frame;
[self.view viewWithTag:1].alpha = 0; //fade transparent black background to clear.
[UIView commitAnimations];
}
-(void)AnimationDidStop:(id) object
{
[self.view removeFromSuperview];
}
hope the additional info helps.
I'm looking for the solution for this issue too.
I found this was the best solution, you can use this SCKit to add tool bar to dismiss the UIPickerView or the UIDatePicker as you want.
Following is github link: https://github.com/scelis/SCKit/tree/
Have fun!