2012-02-03 15 views
5

पर कीबोर्ड के ऊपर टेक्स्ट फ़ील्ड को स्थानांतरित करने के लिए तर्क अभी, मेरे पास संपादन शुरू करते समय कीबोर्ड के ऊपर टेक्स्टफील्ड को स्थानांतरित करने के लिए मूल कोड है। हालांकि, टेक्स्टफील्ड का आकार डिवाइस और अभिविन्यास के आधार पर भिन्न होता है। इसलिए, मैंने इसे करने का एक कच्चा तरीका लिखा है, जो कीबोर्ड के ठीक ऊपर लगातार नहीं रहता है, लेकिन जब आप इसे घुमाते हैं तो इसके बजाय आगे बढ़ेगा, और इसलिए यह पेशेवर जैसा दिखता है उतना दिखता नहीं है।टैप

मेरे प्रश्न का मूल अर्थ यह है कि यदि डिवाइस और अभिविन्यास के आधार पर कीबोर्ड का आकार प्राप्त करने के लिए कोई तर्क है और उस मान का स्वचालित रूप से उपयोग करके और उम्मीद से तेज़ी से तेज़ है।

यदि यह सबसे अच्छा तरीका है, तो कृपया मुझे बताएं। अन्यथा, कृपया इनपुट प्रदान करें। मेरे पास यह कोड है। (यह, बस चाल-अप कोड, नहीं कोड कदम नीचे है बहुत अधिक स्थान ले रोकने के लिए)

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

    //Get Device Type 
    NSString *deviceType = [[UIDevice currentDevice] model]; 

    //Animate Text Field 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.4]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    if ([deviceType isEqualToString:@"iPhone"]) { 

     //Size For iPhone 
     googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 210.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height); 

    } else if ([deviceType isEqualToString:@"iPad"]) { 

     //Size for iPad 
     googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 320.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height); 

    } else if ([deviceType isEqualToString:@"iPod touch"]) { 

     //Size For iPod Touch 
     googleBar.frame = CGRectMake(googleBar.frame.origin.x - 62.0, (googleBar.frame.origin.y - 210.0), googleBar.frame.size.width + 120.0, googleBar.frame.size.height); 

    } 

    [UIView commitAnimations]; 

} 

उत्तर

18

आप जो वास्तव में करना चाहते हैं वह UIKeyboard (Did | Will) (दिखाएँ | छुपाएं) अधिसूचनाओं का पालन करना है। वे अपने userInfo शब्दकोशों में शुरुआत और समापन फ्रेम, साथ ही साथ सही एनीमेशन वक्र और अवधि में शामिल हैं।

तो इस अधिसूचना को देखने के बाद, जब यह पोस्ट किया गया है तो अधिसूचना में पारित फ्रेम के आकार के आधार पर आपके टेक्स्ट फ़ील्ड को स्थानांतरित करें, प्रदान किए गए एनीमेशन संकेतों के अनुसार।

आप UIWindow वर्ग संदर्भ के "अधिसूचना" खंड में अधिक जानकारी देख सकते हैं: https://developer.apple.com/library/ios/#documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html

नीचे एक नमूना दृश्य नियंत्रक कार्यान्वयन है। इस व्यू कंट्रोलर के लिए निब केवल एक टेक्स्ट फ़ील्ड था, जिसमें से जुड़े आउटलेट के साथ, और टेक्स्ट फ़ील्ड का प्रतिनिधि दृश्य नियंत्रक पर सेट था।

@interface ViewController() 

- (void)viewControllerInit; 

@end 

@implementation ViewController 

@synthesize textField; 

- (id)initWithCoder:(NSCoder *)coder { 
    self = [super initWithCoder:coder]; 
    if (self) { 
     [self viewControllerInit]; 
    } 
    return self; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
    { 
     [self viewControllerInit]; 
    } 
    return self; 
} 


- (void)viewControllerInit 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 


- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 


#pragma mark - Notification Handlers 

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    // I'll try to make my text field 20 pixels above the top of the keyboard 
    // To do this first we need to find out where the keyboard will be. 

    NSValue *keyboardEndFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect keyboardEndFrame = [keyboardEndFrameValue CGRectValue]; 

    // When we move the textField up, we want to match the animation duration and curve that 
    // the keyboard displays. So we get those values out now 

    NSNumber *animationDurationNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval animationDuration = [animationDurationNumber doubleValue]; 

    NSNumber *animationCurveNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey]; 
    UIViewAnimationCurve animationCurve = [animationCurveNumber intValue]; 

    // UIView's block-based animation methods anticipate not a UIVieAnimationCurve but a UIViewAnimationOptions. 
    // We shift it according to the docs to get this curve. 

    UIViewAnimationOptions animationOptions = animationCurve << 16; 


    // Now we set up our animation block. 
    [UIView animateWithDuration:animationDuration 
          delay:0.0 
         options:animationOptions 
        animations:^{ 
         // Now we just animate the text field up an amount according to the keyboard's height, 
         // as we mentioned above. 
         CGRect textFieldFrame = self.textField.frame; 
         textFieldFrame.origin.y = keyboardEndFrame.origin.y - textFieldFrame.size.height - 40; //I don't think the keyboard takes into account the status bar 
         self.textField.frame = textFieldFrame; 
        } 
        completion:^(BOOL finished) {}]; 

} 


- (void)keyboardWillHide:(NSNotification *)notification 
{ 

    NSNumber *animationDurationNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval animationDuration = [animationDurationNumber doubleValue]; 

    NSNumber *animationCurveNumber = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey]; 
    UIViewAnimationCurve animationCurve = [animationCurveNumber intValue]; 
    UIViewAnimationOptions animationOptions = animationCurve << 16; 

    [UIView animateWithDuration:animationDuration 
          delay:0.0 
         options:animationOptions 
        animations:^{ 
         self.textField.frame = CGRectMake(20, 409, 280, 31); //just some hard coded value 
        } 
        completion:^(BOOL finished) {}]; 

} 
#pragma mark - View lifecycle 

- (void)viewDidUnload 
{ 
    [self setTextField:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

#pragma mark - UITextFieldDelegate 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [self.textField resignFirstResponder]; 
    return YES; 
} 

@end 
+0

मैं इसे इस तरह से कर सकता था, और मैं अभी अलर्ट अनुभाग देख रहा हूं। हालांकि, आप पोस्ट किए गए तरीके में इसे लागू करने के लिए उन संदेशों से वास्तविक निर्देशांक प्राप्त करने के बारे में कैसे जाएंगे। मैं अभी इसके साथ झुका रहा हूं, लेकिन सहायता अच्छी होगी। – JTApps

+0

अपडेट: मैं पिछले 35 मिनट से इस पर काम कर रहा हूं। अब तक, मेरे पास इसके लिए कुछ भी दिखाने के लिए नहीं है। क्या आप संभवतः मुझे चेक मार्क के लिए कुछ सहायता प्रदान कर सकते हैं? बहुत सराहना की! – JTApps

+0

चेक मार्क से आपका क्या मतलब है? हालांकि मैं थोड़ा सा बुनियादी कोड पोस्ट करूंगा। –

0

UIViewControllers एक संपत्ति interfaceOrientation और समारोह UIInterfaceOrientationIsPortrait/लैंडस्केप आप कर सकते हैं कहा जाता है तो अनिवार्य रूप से है करें:

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation){ 
//portrait logic 
} 
else{ 
//landcapeLogic 
} 

आपके दृश्य नियंत्रक में प्रत्येक आईफोन और आईपैड के अंदर अंदर। वहां से आप अपने पिक्सेल माप तरीके को कर सकते हैं जैसा कि आपने पहले किया था, क्योंकि जहां तक ​​मुझे पता है कि यह करने का सबसे आसान तरीका है।

पीएस लैंडस्केप चेकर की जांच करने के लिए एक फ़ंक्शन भी है, लेकिन यदि पहला अगर कथन गलत है तो डिवाइस पोर्ट्रेट में नहीं है, तो यह परिदृश्य में होना चाहिए और इसलिए सादा है।

+0

परिदृश्य के बारे में क्या है, साथ ही साथ उल्टा?या, क्या पोर्टेट और परिदृश्य गुणों में उतार-चढ़ाव के साथ-साथ दोनों परिदृश्य विकल्प शामिल हैं? – JTApps

+0

पोर्ट्रेट कुछ भी सही तरफ ऊपर या ऊपर की ओर जाता है, और परिदृश्य दोनों बाएं और दाएं – brandonbocklund

+0

अच्छा कवर करता है, मैं इसे आज़माउंगा, और यदि यह काम करता है, तो मैं आपको चेक मार्क दूंगा। मुझे एक मिनट दें कृपया =) – JTApps

2

अधिसूचना कुंजीपटल रजिस्टर का उपयोग करके आप एक स्क्रॉल के अंदर अपने पाठ फ़ील्ड जगह है और सामग्री पुस्तक की भरपाई कीबोर्ड के ऊपर वास्तविक पहली प्रत्युत्तर चालू करने के लिए अगर यह आवश्यक है प्रबंधन कर सकते हैं।

तो कुंजीपटल के लिए रजिस्टर नियंत्रक के बाद, आपको कीबोर्ड मूल के बीच का अंतर प्राप्त करना होगा और माता-पिता के सापेक्ष मूल स्क्रॉल करना होगा।

आपको पता होना चाहिए कि कोई विशिष्ट प्रथम उत्तरदाता स्क्रॉल की सामग्री ऑफ़सेट को बदल सकता है, इसलिए कुंजीपटल मूल और पहले उत्तरदाता के बीच संभावित सीमाओं को जानने के लिए निष्पक्ष है।

जिस तरह से आपको स्क्रॉल सामग्री ऑफ़सेट betfen अंतराल और पहले उत्तरदाता को विशिष्ट स्थिति में अपना पहला उत्तरदाता रखने के लिए आवश्यक है।

@interface MainViewController : UIViewController 

    @property (strong, nonatomic) IBOutlet UIScrollView *scroll; 

@end  

@interface MainViewController() 
{ 
    CGPoint scrollOffset; 
} 
@end 

@implementation MainViewController 

@synthesize scroll 

-(void)viewWillAppear:(BOOL)animated 
{ 
    [[ NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardDidShowNotification object:nil]; 

    [[ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillDisAppear:) name:UIKeyboardDidHideNotification object:nil]; 

} 


-(void)viewWillDisappear:(BOOL)animated 
{ 
    [[ NSNotificationCenter defaultCenter ] removeObserver:self ]; 
} 

-(void)keyboardWillAppear:(NSNotification*) note 
{ 
    const CGFloat default_gap = 25.0f; 

    NSValue *keyBoardEndFrameValue = [[ note userInfo ] objectForKey:UIKeyboardFrameEndUserInfoKey ]; 

    CGRect keyBoardFrame = [ keyBoardEndFrameValue CGRectValue ]; 


    offset = scroll.contentOffset; 

    UIWindow *window = [[ UIApplication sharedApplication ] keyWindow]; 
    UITextField *textField = (UITextField*)[ window performSelector:@selector(firstResponder) ]; 

    //Gap between keyboard origin and the scroll origin, relative to parent. 
    CGFloat distanceRelativeToParent = keyBoardFrame.origin.y - scroll.frame.origin.y; 


    //Distance between superview to textfield inside scroll. to determine if it's necesary to scroll. 
    CGFloat bound = (textField.frame.origin.y + textField.frame.size.height)+scroll.frame.origin.y; 

    CGFloat gapScroll = textField.frame.size.height+default_gap; 

    if(bound >= keyBoardFrame.origin.y) 
    { 
    [ UIView animateWithDuration:.3 delay:0.0 options:UIViewAnimationCurveEaseOut 
    animations:^{ 

     [ scroll setContentOffset:CGPointMake(0, textField.frame.origin.y - distanceRelativeToParent + gapScroll ) animated:YES ]; 

    } 
    completion:^(BOOL finished){ 

    }]; 
} 

} 

-(void) keyboardWillDisAppear:(NSNotification*) note 
{ 
    [ scroll setContentOffset:offset animated:YES ]; 
} 
@end 
संबंधित मुद्दे