I am not able to sort of where is the mistake in my code . When i click the "Submit" button then the keyboard rises again. Here i am using the NSNotificatioCenter which scrolls the view according to keyboard height.
Have a look on code below.
-(void)viewDidLoad
{
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES];
// Register for the events
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
// Setup content size
_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,SCROLLVIEW_CONTENT_HEIGHT);
keyboardVisible = NO;
}
-(void) viewWillDisappear:(BOOL)animated
{
NSLog (#"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void) keyboardDidShow: (NSNotification *)notif
{
NSLog(#"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(#"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = _scrollView.contentOffset;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = _scrollView.frame;
viewFrame.size.height -= keyboardSize.height;
_scrollView.frame = viewFrame;
CGRect textFieldRect = [activeView frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible:textFieldRect animated:YES];
NSLog(#"ao fim");
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(#"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
// Reset the scrollview to previous location
_scrollView.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(BOOL) textViewShouldBeginEditing:(UITextView*)textView {
activeView = textView;
return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView{
if(textView == _textViewFeedback)
[_textViewFeedback resignFirstResponder];
else
[_textViewEmail resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if([text isEqualToString:#"\n"]){
[textView resignFirstResponder];
}
return YES;
}
- (IBAction)BtnActionSubmitFeedback:(id)sender {
_textViewEmail.text = nil;
_textViewFeedback.text = nil;
NSString *errorMessage = [self validateForm];
if (errorMessage) {
showAlert(#"Warning", errorMessage, nil, nil, #"Dismiss");
return;
}
}
Add a delegate of UITextField
#interface MyViewController : UIViewController <UITextFieldDelegate>
Now set your textField.delegate = self; in viewDidLoad
Add this delegate method to dismiss Keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
OR
You may Put [self.view endEditing:YES]; in your Submit button action to dismiss Keyboard.
In my code by default work UITapGesture how to remove this
NSString *dirString;
host is class object
-(Void) handleSwipeEvent : (UISwipeGestureRecognizer *) recognizer {
//NSString *swipeDirection = NULL;
//CGPoint location = [recognizer locationInView:self];
dirString = NULL;
UITapGestureRecognizer *tap=nil;
tap.enabled=NO;
if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {
dirString = kCATransitionFromRight;
}
if ([recognizer direction] == UISwipeGestureRecognizerDirectionRight)
{
dirString = kCATransitionFromLeft;
}
if (dirString) [host swipeTo:dirString];
}
-(void)createSwipeGestureRecognizer:(UIView *)view direction: (UISwipeGestureRecognizerDirection)direction
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeEvent:)];
[view addGestureRecognizer:recognizer];
[recognizer setDirection:direction];
[self setSwipeRecognizer:recognizer];
[recognizer release];
}
- (void) setHost: (FlashCardViewController *) aHost {
host = aHost;
[self createSwipeGestureRecognizer:self direction:UISwipeGestureRecognizerDirectionLeft];
[self createSwipeGestureRecognizer:self direction:UISwipeGestureRecognizerDirectionRight];
}
On the view i have to touch then load next contant.
How can i disable tap event
I am working with IOS 6 UITableView and my problem is that whenever i put the table view into edit mode, the edit control that appears next to the table view cell of deletestyle ( UITableViewCellEditingStyleDelete ) overlaps two labels ( xyzname and xyzcollege ).
I did not have this problem in IOS 5 (with same code).
CODE:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]pathForResource:#"friends"ofType:#"plist"];
NSDictionary *xyz = [[NSDictionary alloc]initWithContentsOfFile:path];
_names = [xyz objectForKey:#"name"];
_colls = [xyz objectForKey:#"college"];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
NSInteger count =_names.count;
if(self.editing)
{
count++;
}
return count;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
saumyaTableViewCell *cell;
cell = [_saumyaTable dequeueReusableCellWithIdentifier:#"saumyacell"];
if(indexPath.row < _names.count)
{
cell.xyzname.text = [_names objectAtIndex:indexPath.row];
cell.xyzcollege.text = [_colls objectAtIndex:indexPath.row];
}
else
{
cell.xyzname.text = #"Add name.";
cell.xyzcollege.text = #"Add College";
cell.editingAccessoryType =
UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(void)setEditing:(BOOL)editing animated:(BOOL) animated
{
if( editing != self.editing )
{
[super setEditing:editing animated:animated];
[_saumyaTable setEditing:editing animated:animated];
NSArray *indexes = [NSArray arrayWithObject:
[NSIndexPath indexPathForRow:_names.count inSection:0]];
if (editing == YES )
{
[_saumyaTable insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationLeft];
}
else
{
[_saumyaTable deleteRowsAtIndexPaths:indexes
withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < _names.count)
{
return UITableViewCellEditingStyleDelete;
}
else
{
return UITableViewCellEditingStyleInsert;
}
}
IMAGE DESCRIPTION:
1.UITableViewCell has two labels(xyzname and xyzcollege)
2.UITableViewCellEditingStyleDelete overlaps these two labels when cell goes into edit mode
What is to be done if i want those labels to automatically animate on the right side when my view goes into the edit mode and again animate to their original positions after coming out of the edit mode?
THANK YOU
I have a requirement to adding a path in my MKMapview between two annotation pin. There is no issue with ios 5 or older but when i try to run app in ios 6 app is surprisingly quit. below is my code. If there is any correction in my code then please suggest me.
1.So my question is suggest the best way how to decrees memory in my following code so that i can solve crash in my Application.
- (void)viewDidLoad
{
NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoints:pathArray mapView:_mapView];
[_mapView addAnnotation:annotation];
[annotation release];
[pathArray removeAllObjects];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"%s",__FUNCTION__);
if ([annotation isKindOfClass:[NVPolylineAnnotation class]])
{
NSLog(#"=========ANOTATION=========NVPolylineAnnotationView START");
//ann=[ann initWithAnnotation:annotation mapView:_mapView];
NVPolylineAnnotationView *ann=[[NVPolylineAnnotationView alloc] init];
return [[ann initWithAnnotation:annotation mapView:_mapView] autorelease];//[[[NVPolylineAnnotationView alloc] initWithAnnotation:annotation mapView:_mapView] autorelease];
}
else if([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pointers"] ;
// annView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annView.animatesDrop=NO;
annView.canShowCallout = TRUE;
return [annView autorelease];
}
else if([annotation isKindOfClass:[PlacePin class]])
{
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
}
return nil;
}
- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
NSLog(#"%s",__FUNCTION__);
// fixes that some marker are behind the polyline
for (int i=0; i<[views count]; i++)
{
MKAnnotationView *view = [views objectAtIndex:i];
if ([view isKindOfClass:[NVPolylineAnnotationView class]])
{
[[view superview] sendSubviewToBack:view];
/* In iOS version above 4.0 we need to update the polyline view after it
has been added to the mapview and it ready to be displayed. */
NSString *reqSysVer = #"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
[self updatePolylineAnnotationView];
}
}
}
NSLog(#"----------didAddAnnotationViews");
}
- (void)updatePolylineAnnotationView
{
NSLog(#"%s",__FUNCTION__);
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
[mapView selectAnnotation:mp animated:NO];
*/
for (NSObject *a in [_mapView annotations])
{
if ([a isKindOfClass:[NVPolylineAnnotation class]])
{
NVPolylineAnnotation *polyline = (NVPolylineAnnotation *)a;
NSObject *pv = (NSObject *)[_mapView viewForAnnotation:polyline];
if ([pv isKindOfClass:[NVPolylineAnnotationView class]])
{
NVPolylineAnnotationView *polylineView =
(NVPolylineAnnotationView *)[_mapView viewForAnnotation:polyline];
[polylineView regionChanged];
}
}
}
}
I have a form on the ipad which has many textfield and a button at the end. There are some fields which come under the keyboard when it is active. In order to pull the hidden texfield behind the keyboard to be visible I am using the following code.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_scrollView.frame=CGRectMake(0, 0, 1024, 655);
[self animateTextField:textField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
{
// NSLog(#"portrait");
if(up)
{
int moveUpValue = temp.y+textField.frame.size.height;
animatedDis = 264-(1024-moveUpValue-5);
}
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
if(up)
{
int moveUpValue = 1004-temp.y+textField.frame.size.height;
animatedDis = 264-(1004-moveUpValue-5);
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
if(up)
{
int moveUpValue = temp.x+textField.frame.size.height;
animatedDis = 352-(768-moveUpValue-5);
}
}
else
{
if(up)
{
int moveUpValue = 768-temp.x+textField.frame.size.height;
animatedDis = 352-(768-moveUpValue-5);
_scrollView.frame = CGRectMake(0, 0, 1024, 655-240);
}
}
if(animatedDis>0)
{
const int movementDistance = animatedDis;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
if (orientation == UIInterfaceOrientationPortrait)
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
else
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
[UIView commitAnimations];
}
}
I am also using the scroll view. My issue is that when the keyboard is active and i press my button it take me to the next screen. Now if i navigate back, the keyboard is active and the prior animations are set. Now if i hide the keyboard, the entire view scrolls down leaving a black portion on top. How to handle this situation?
Okay. After much research I found a simple method. It is the use of notifications.
In my viewDidLoad i added these two keyboard notifications.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
These are the 2 selector methods:
-(void)keyboardWasShown:(NSNotification *)aNotification
{
if (displayKeyboard==YES) {
return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSLog(#"kbw====%fkbh====%f",keyboardSize.width,keyboardSize.height);
offset = _scrollView.contentOffset;
CGRect viewFrame = _scrollView.frame;
NSLog(#"svw====%fsvh===%f",viewFrame.size.width,viewFrame.size.height);
viewFrame.size.height -= keyboardSize.height-49;
NSLog(#"new view hgt =====%f",viewFrame.size.height);
_scrollView.frame = viewFrame;
CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;
}
-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{
if (!displayKeyboard) {
return;
}
_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}
displayKeyboard, offset and activeField are declared in .h file.
Also remember to remove the notifications in viewDidDisappear:animated
Although this method is quite different than the previous one stated, this one does not leave a black portion on the top while navigating between classes when the uikeyboard is active.
Also what i noticed was if i used the deprecated UIKeyboardBoundsUserInfoKey i used to get the correct width and height of the keyboard(i am working only in landscape mode). Whereas when i used UIKeyboardFrameBeginUserInfoKey the width and height values were interchanged. I am still trying to figure out this problem.
Also when the keyboard used to appear, a fixed space of 49px was appended to above it. I assumed that that was my tabbar height and therefore subtracted 49.