2009-12-23 30 views
9

मेरे पास मेरे दृश्य के नीचे एक UIToolbar है, और मेरे पास इस टूलबार में UITextField है। जब मैं इस क्षेत्र को संपादित करना शुरू करता हूं, तो यह कीबोर्ड के पीछे छिपा हुआ है। मैंने जो टाइप किया है उसे देखने के लिए, मैं कुंजीपटल प्रस्तुत किए जाने पर टूलबार को ऊपर ले जाना चाहता हूं (और फिर संपादन समाप्त होने पर इसे वापस ले जाएं)।ऊपर UIToolbar

मैं इस UIToolbar को ऊपर/नीचे कैसे स्थानांतरित करूं?

+0

http://stackoverflow.com/questions/1247113/iphone-keyboard-covers-text-field/1533847#1533847 – cidered

+0

डुप्लिकेट नहीं है की डुप्लिकेट। यह प्रश्न टूलबार के बारे में है जो टेक्स्ट फ़ील्ड नहीं है। – phatmann

उत्तर

13

UIKeyboardWillShowNotification/UIKeyboardWillHideNotification के पर्यवेक्षकों की सूची में अपना व्यू कंट्रोलर क्लास जोड़ें। तो आप अपना टेक्स्ट दृश्यमान बनाने के लिए अपना दृश्य स्थानांतरित कर सकते हैं। आप वर्तमान ओएस संस्करण के कीबोर्ड एनीमेशन पैरामीटर के साथ अपनी एनीमेशन को सिंक्रनाइज़ करने के लिए इस नोटिफिकेशन से एनीमेशन पैरामीटर भी प्राप्त कर सकते हैं। इस कोड को मैं पेजिंग

- (void) viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(liftMainViewWhenKeybordAppears:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnMainViewToInitialposition:) name:UIKeyboardWillHideNotification object:nil]; 
} 
- (void) viewWillDisappear:(BOOL)animated{ 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
तरीकों में

के लिए उपयोग किया है नीचे मैं कुंजीपटल अधिसूचना को संभालने के लिए दो तरीकों की स्थापना की। और यहाँ इस तरीके हैं:

- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{ 
    NSDictionary* userInfo = [aNotification userInfo]; 
    NSTimeInterval animationDuration; 
    UIViewAnimationCurve animationCurve; 
    CGRect keyboardFrame; 
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; 
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame]; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    [UIView setAnimationCurve:animationCurve]; 
    [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)]; 
    [UIView commitAnimations]; 
} 
- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{ 
    NSDictionary* userInfo = [aNotification userInfo]; 
    NSTimeInterval animationDuration; 
    UIViewAnimationCurve animationCurve; 
    CGRect keyboardFrame; 
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; 
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame]; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    [UIView setAnimationCurve:animationCurve]; 
    [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)]; 
    [UIView commitAnimations]; 
} 
+0

अच्छे समाधान के लिए धन्यवाद। लेकिन मुझे अभी भी टूलबार को स्थानांतरित करने में परेशानी है। दृश्य चलता है लेकिन टूलबार नहीं जिसे मैंने 'viewDidLoad'method में बनाया है। क्या आप मुझे संकेत दे सकते हैं?धन्यवाद – JFS

8

धन्यवाद कि काम किया! यहाँ एक मामूली सुधार है:

- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{ 
    [self scrollViewForKeyboard:aNotification up:YES]; 
} 

- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{ 
    [self scrollViewForKeyboard:aNotification up:NO]; 
} 

- (void) scrollViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{ 
    NSDictionary* userInfo = [aNotification userInfo]; 

    // Get animation info from userInfo 
    NSTimeInterval animationDuration; 
    UIViewAnimationCurve animationCurve; 
    CGRect keyboardFrame; 
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; 
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame]; 

    // Animate up or down 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:animationDuration]; 
    [UIView setAnimationCurve:animationCurve]; 

    [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + (keyboardFrame.size.height * (up?-1:1)), self.view.frame.size.width, self.view.frame.size.height)]; 
    [UIView commitAnimations]; 
} 
2

आप इस के लिए बहुत बहुत धन्यवाद, यह बहुत अच्छा काम करता है।

1) दृश्य बस बल्कि कुंजीपटल के बाद स्थान उपलब्ध फिट करने के लिए आकार बदलने से स्क्रीन के बाहर ऊपर स्लाइड पुनर्निर्धारित किया जा रहा प्रतीत होता है

2) दोहराएँ: हालांकि, के रूप में प्रस्तुत कोड 2 सीमाओं के रूप में मैं उन्हें अनुभव है टेक्स्ट फ़ील्ड स्विच करने के कारण नोटिफिकेशन फ्रेम परिवर्तन को लागू करना जारी रखता है, जिससे दृश्य स्क्रीन को उभरने के लिए दृश्य बनाता है।

कारण यह है कि उपरोक्त कीबोर्ड के सापेक्ष आकार बदलने के बजाय दृश्य के वर्तमान फ्रेम के सापेक्ष एक प्रतिस्थापन है।

liftMainViewWhenKeybordAppears में पुन: आकार बदलने के बजाय :,, कीबोर्ड के सापेक्ष: (अनिवार्य रूप से समान:

keyboardFrame = [self.view.window convertRect:keyboardFrame toView:self.view.superview]; 
    CGRect superviewFrame = [self.view.window convertRect:self.view.superview.frame toView:self.view]; 
    [self.view setFrame:CGRectMake(self.view.frame.origin.x, 
           self.view.frame.origin.y, 
           self.view.frame.size.width, 
           superviewFrame.size.height - keyboardFrame.size.height)]; 

returnMainViewToInitialposition में :, इस setFrame को एनीमेशन बदलने के यहाँ कोड के दो संशोधन लाइनों जो ठीक इस हैं पहचान परिवर्तन के लिए)।

[self.view setFrame:CGRectMake(self.view.frame.origin.x, 
           self.view.frame.origin.y, 
           self.view.frame.size.width, 
           keyboardFrame.origin.y + keyboardFrame.size.height)]; 
0

यहाँ एक अधिक सुरुचिपूर्ण समाधान UIView श्रेणी

#import <Foundation/Foundation.h> 


@interface UIView(AnimationUtils) 
-(void)scrollControlToCenter:(UIView *)view; 
-(void)scrollViewToOriginalPosition; 
@end 



#import "UIView+AnimationUtils.h" 


@implementation UIView(AnimationUtils) 
#pragma mark ScrollView Methods 

//Method Called whenever keyboard appears 
- (void)scrollControlToCenter:(UIView *)view { 
    if([view isKindOfClass:[UITextField class]]){ 
     CGRect viewFrame = [view frame]; 
     float verticalDistance = 216.0f - viewFrame.origin.y - (2*viewFrame.size.height); 
     if(viewFrame.size.height >= (460 - 216)/2){ 
      verticalDistance = 0; 
     } 
     [UIView beginAnimations:@"ScrollToCenter" context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     [self setFrame:CGRectMake(0, verticalDistance, self.frame.size.width, self.frame.size.height)]; 
     [UIView commitAnimations]; 
    }else if([view isKindOfClass:[UITextView class]]){ 
     [UIView beginAnimations:@"ScrollToTop" context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     UIView *viewBG = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
     [viewBG setTag:5]; 
     [viewBG setBackgroundColor:[UIColor blackColor]]; 
     [viewBG setAlpha:0.75]; 
     [self addSubview:viewBG]; 
     [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)]; 
     [self setFrame:CGRectMake(0, -view.frame.origin.y , self.frame.size.width, self.frame.size.height)]; 
     [self insertSubview:view atIndex:[self.subviews count] + 1]; 
     [UIView commitAnimations]; 
    } 

} 
-(void)scrollViewToOriginalPosition{ 
    [UIView beginAnimations:@"ScrollToOriginal" context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    for(UIView *view in self.subviews){ 
     if(view.tag == 5){ 
      [view removeFromSuperview]; 
     } 
    } 
    [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    [UIView commitAnimations]; 

} 

#pragma mark - 
@end 
5

भवन का उपयोग करके उपरोक्त जवाब पर और सुविधा विधि का उपयोग कर रहा है [UIView animateWithDuration ...]। शो को कीबोर्ड नोटिफिकेशन दिखाएं/छुपाएं और इन हैंडलर का उपयोग करें।

- (void)keyboardWillShow:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey]; 
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey]; 

    [UIView animateWithDuration:durationValue.doubleValue 
          delay:0 
         options:(curveValue.intValue << 16) 
        animations:^{ 
         self.navigationController.toolbar.frame = CGRectMake(0, 
                       [endFrame CGRectValue].origin.y - self.navigationController.toolbar.bounds.size.height, 
                       self.navigationController.toolbar.bounds.size.width, 
                       self.navigationController.toolbar.bounds.size.height); 
        } 
        completion:nil]; 
} 

- (void)keyboardWillHide:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey]; 

    [UIView animateWithDuration:durationValue.doubleValue 
          delay:0 
         options:(curveValue.intValue << 16) 
        animations:^{ 
         self.navigationController.toolbar.frame = CGRectMake(0, 
                       self.view.bounds.size.height - self.navigationController.toolbar.bounds.size.height, 
                       self.navigationController.toolbar.bounds.size.width, 
                       self.navigationController.toolbar.bounds.size.height); 
        } 
        completion:nil]; 
} 
+0

UIViewAnimationCurve कास्टिंग UIViewAnimationOptionCurve में स्थानांतरित करके! मैं इस के साथ कैसे आ सकता था !? बहुत बहुत धन्यवाद। –

संबंधित मुद्दे