एक UITableView स्क्रॉल बनाना जब पाठ क्षेत्र का चयन किया गया

वोट
228

परीक्षण और त्रुटि के एक बहुत बाद, मैं दे रहा हूँ और सवाल पूछने। मैं इसी तरह की समस्याओं के साथ बहुत से लोगों को देखा है लेकिन सभी जवाब सही काम करने के लिए नहीं मिल सकता है।

मैं एक है UITableViewजो कस्टम कोशिकाओं से बना है। कोशिकाओं (एक ग्रिड की तरह तरह की) एक दूसरे के बगल 5 पाठ फ़ील्ड के बने होते हैं।

जब मैं स्क्रॉल और के निचले भाग में कोशिकाओं संपादित करने का प्रयास UITableViewहै, मैं प्राप्त करने के लिए मेरी कोशिकाओं को ठीक कुंजीपटल ऊपर तैनात प्रबंधन नहीं कर सकते।

मैं बदलते दृश्य आकार, आदि के बारे में बात कई जवाब देखा है ... लेकिन उनमें से कोई अच्छी तरह से अब तक काम किया है।

किसी को भी सही जिस तरह से एक ठोस कोड उदाहरण के साथ यह करने के लिए स्पष्ट कर सकता है?

27/02/2009 को 11:05
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


48 जवाब

वोट
110

आप UIViewController के बजाय UITableViewController का उपयोग करते हैं, यह स्वतः ही ऐसा करेंगे।

21/09/2010 को 04:42
का स्रोत उपयोगकर्ता

वोट
89

समारोह है कि स्क्रॉल करता है बहुत सरल हो सकता है:

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    UITableViewCell *cell;

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
    // Load resources for iOS 6.1 or earlier
        cell = (UITableViewCell *) textField.superview.superview;

    } else {
        // Load resources for iOS 7 or later
        cell = (UITableViewCell *) textField.superview.superview.superview; 
       // TextField -> UITableVieCellContentView -> (in iOS 7!)ScrollView -> Cell!
    }
    [tView scrollToRowAtIndexPath:[tView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

बस। सब पर कोई गणना।

15/04/2009 को 13:21
का स्रोत उपयोगकर्ता

वोट
65

मैं बहुत कुछ इसी तरह यह सामान्य है कर रहा हूँ, कुछ अपने कोड के लिए विशिष्ट गणना करने के लिए कोई जरूरत नहीं। बस कोड पर टिप्पणी की जाँच करें:

MyUIViewController.h में

@interface MyUIViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
{
     UITableView *myTableView;
     UITextField *actifText;
}

@property (nonatomic, retain) IBOutlet UITableView *myTableView;
@property (nonatomic, retain) IBOutlet UITextField *actifText;

- (IBAction)textFieldDidBeginEditing:(UITextField *)textField;
- (IBAction)textFieldDidEndEditing:(UITextField *)textField;

-(void) keyboardWillHide:(NSNotification *)note;
-(void) keyboardWillShow:(NSNotification *)note;

@end

MyUIViewController.m में

@implementation MyUIViewController

@synthesize myTableView;
@synthesize actifText;

- (void)viewDidLoad 
{
    // Register notification when the keyboard will be show
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(keyboardWillShow:)
                                          name:UIKeyboardWillShowNotification
                                          object:nil];

    // Register notification when the keyboard will be hide
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(keyboardWillHide:)
                                          name:UIKeyboardWillHideNotification
                                          object:nil];
}

// To be link with your TextField event "Editing Did Begin"
//  memoryze the current TextField
- (IBAction)textFieldDidBeginEditing:(UITextField *)textField
{
    self.actifText = textField;
}

// To be link with your TextField event "Editing Did End"
//  release current TextField
- (IBAction)textFieldDidEndEditing:(UITextField *)textField
{
    self.actifText = nil;
}

-(void) keyboardWillShow:(NSNotification *)note
{
    // Get the keyboard size
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

    // Detect orientation
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect frame = self.myTableView.frame;

    // Start animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];

    // Reduce size of the Table view 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
        frame.size.height -= keyboardBounds.size.height;
    else 
        frame.size.height -= keyboardBounds.size.width;

    // Apply new size of table view
    self.myTableView.frame = frame;

    // Scroll the table view to see the TextField just above the keyboard
    if (self.actifText)
      {
        CGRect textFieldRect = [self.myTableView convertRect:self.actifText.bounds fromView:self.actifText];
        [self.myTableView scrollRectToVisible:textFieldRect animated:NO];
      }

    [UIView commitAnimations];
}

-(void) keyboardWillHide:(NSNotification *)note
{
    // Get the keyboard size
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue: &keyboardBounds];

    // Detect orientation
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect frame = self.myTableView.frame;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];

    // Increase size of the Table view 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
        frame.size.height += keyboardBounds.size.height;
    else 
        frame.size.height += keyboardBounds.size.width;

    // Apply new size of table view
    self.myTableView.frame = frame;

    [UIView commitAnimations];
}

@end

स्विफ्ट 1.2+ संस्करण:

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var activeText: UITextField!
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("keyboardWillShow:"),
            name: UIKeyboardWillShowNotification,
            object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("keyboardWillHide:"),
            name: UIKeyboardWillHideNotification,
            object: nil)
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        activeText = textField
    }

    func textFieldDidEndEditing(textField: UITextField) {
        activeText = nil
    }

    func keyboardWillShow(note: NSNotification) {
        if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            var frame = tableView.frame
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationBeginsFromCurrentState(true)
            UIView.setAnimationDuration(0.3)
            frame.size.height -= keyboardSize.height
            tableView.frame = frame
            if activeText != nil {
                let rect = tableView.convertRect(activeText.bounds, fromView: activeText)
                tableView.scrollRectToVisible(rect, animated: false)
            }
            UIView.commitAnimations()
        }
    }

    func keyboardWillHide(note: NSNotification) {
        if let keyboardSize = (note.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            var frame = tableView.frame
            UIView.beginAnimations(nil, context: nil)
            UIView.setAnimationBeginsFromCurrentState(true)
            UIView.setAnimationDuration(0.3)
            frame.size.height += keyboardSize.height
            tableView.frame = frame
            UIView.commitAnimations()
        }
    }
}
13/04/2010 को 15:46
का स्रोत उपयोगकर्ता

वोट
41

मैं एक ही समस्या थी लेकिन गौर किया है कि यह केवल एक दृश्य में दिखाई देता है। इसलिए मैं नियंत्रकों में मतभेद देखने के लिए शुरू कर दिया।

मुझे पता चला है कि स्क्रॉल व्यवहार में सेट है - (void)viewWillAppear:(BOOL)animatedसुपर उदाहरण के।

तो इस तरह से लागू करने के लिए सुनिश्चित हो:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // your code
}

और यह कोई फर्क नहीं पड़ता अगर आप का उपयोग करें UIViewControllerया UITableViewController; एक रख कर यह जाँच UITableViewमें self.view की एक subview के रूप UIViewController। यह वही व्यवहार किया गया था। दृश्य अगर कॉल स्क्रॉल करने के लिए अनुमति नहीं दी [super viewWillAppear:animated];याद आ रही थी।

29/05/2011 को 01:42
का स्रोत उपयोगकर्ता

वोट
37

मैं इस चूक हो सकती है, के रूप में मैं पूरी पोस्ट यहाँ पढ़ नहीं था, लेकिन मैं क्या के साथ आया था भ्रामक सरल लगता है। मैं इस wringer के माध्यम से नहीं डाल दिया है, सभी परिस्थितियों में परीक्षण, लेकिन ऐसा लगता है जैसे कि यह ठीक काम करना चाहिए।

बस कीबोर्ड की ऊंचाई से tableview की contentInset समायोजित, और फिर नीचे करने के लिए सेल स्क्रॉल:

- (void)keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.myTableView.contentInset = contentInsets;
    self.myTableView.scrollIndicatorInsets = contentInsets;

    [self.myTableView scrollToRowAtIndexPath:self.currentField.indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

और निश्चित रूप से

- (void)keyboardWasHidden:(NSNotification *)aNotification
{
    [UIView animateWithDuration:.3 animations:^(void) 
    {
        self.myTableView.contentInset = UIEdgeInsetsZero;
        self.myTableView.scrollIndicatorInsets = UIEdgeInsetsZero;
    }];
}

यह भी सरल है? क्या मैं कुछ भूल रहा हूँ? अब तक यह मेरे लिए काम कर रहा है ठीक है, लेकिन जैसा कि मैंने कहा, मैं इसे wringer के माध्यम से नहीं डाल दिया है ...

18/08/2012 को 01:12
का स्रोत उपयोगकर्ता

वोट
35

के लिए सरल समाधान स्विफ्ट 3 , के आधार पर Bartłomiej Semańczyk समाधान :

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(CreateEditRitualViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(CreateEditRitualViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

// MARK: Keyboard Notifications

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
        tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    UIView.animate(withDuration: 0.2, animations: {
        // For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    })
}
08/12/2016 को 13:26
का स्रोत उपयोगकर्ता

वोट
34

आप उपयोग कर सकते हैं UITableViewController, आप मुक्त करने के लिए कार्यक्षमता प्राप्त। कभी कभी, तथापि, यह एक विकल्प नहीं है विशेष रूप से यदि आप कई बार देखा गया बस नहीं की जरूरत है, UITableView

यहां प्रस्तुत समाधान के कुछ आईओएस पर काम नहीं करते ≥4, कुछ आईपैड पर या लैंडस्केप मोड में काम नहीं करते, कुछ ब्लूटूथ कीबोर्ड के लिए काम नहीं करते (जहां हम किसी भी स्क्रॉल नहीं करना चाहती), कुछ नहीं काम जब एक से अधिक पाठ क्षेत्रों के बीच स्विच। तो अगर आप किसी भी समाधान चुनते हैं, तो इन मामलों का परीक्षण करने के लिए सुनिश्चित करें। यह समाधान हम है उपयोग के लिए इस्तेमाल किया में InAppSettingsKit :

- (void)_keyboardWillShow:(NSNotification*)notification {
    if (self.navigationController.topViewController == self) {
        NSDictionary* userInfo = [notification userInfo];

        // we don't use SDK constants here to be universally compatible with all SDKs ≥ 3.0
        NSValue* keyboardFrameValue = [userInfo objectForKey:@"UIKeyboardBoundsUserInfoKey"];
        if (!keyboardFrameValue) {
            keyboardFrameValue = [userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"];
        }

        // Reduce the tableView height by the part of the keyboard that actually covers the tableView
        CGRect windowRect = [[UIApplication sharedApplication] keyWindow].bounds;
        if (UIInterfaceOrientationLandscapeLeft == self.interfaceOrientation ||UIInterfaceOrientationLandscapeRight == self.interfaceOrientation ) {
            windowRect = IASKCGRectSwap(windowRect);
        }
        CGRect viewRectAbsolute = [_tableView convertRect:_tableView.bounds toView:[[UIApplication sharedApplication] keyWindow]];
        if (UIInterfaceOrientationLandscapeLeft == self.interfaceOrientation ||UIInterfaceOrientationLandscapeRight == self.interfaceOrientation ) {
            viewRectAbsolute = IASKCGRectSwap(viewRectAbsolute);
        }
        CGRect frame = _tableView.frame;
        frame.size.height -= [keyboardFrameValue CGRectValue].size.height - CGRectGetMaxY(windowRect) + CGRectGetMaxY(viewRectAbsolute);

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
        [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
        _tableView.frame = frame;
        [UIView commitAnimations];

        UITableViewCell *textFieldCell = (id)((UITextField *)self.currentFirstResponder).superview.superview;
        NSIndexPath *textFieldIndexPath = [_tableView indexPathForCell:textFieldCell];

        // iOS 3 sends hide and show notifications right after each other
        // when switching between textFields, so cancel -scrollToOldPosition requests
        [NSObject cancelPreviousPerformRequestsWithTarget:self];

        [_tableView scrollToRowAtIndexPath:textFieldIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

- (void) scrollToOldPosition {
  [_tableView scrollToRowAtIndexPath:_topmostRowBeforeKeyboardWasShown atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)_keyboardWillHide:(NSNotification*)notification {
    if (self.navigationController.topViewController == self) {
        NSDictionary* userInfo = [notification userInfo];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
        [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
        _tableView.frame = self.view.bounds;
        [UIView commitAnimations];

        [self performSelector:@selector(scrollToOldPosition) withObject:nil afterDelay:0.1];
    }
}   

यहाँ वर्ग से भरा कोड InAppSettingsKit में। यह परीक्षण करने के लिए, "पूरी सूची" बच्चे फलक जहां ऊपर उल्लेख किया परिदृश्यों का परीक्षण कर सकते हैं का उपयोग करें।

13/12/2010 को 17:01
का स्रोत उपयोगकर्ता

वोट
34

मुझे लगता है मैं समाधान एप्पल के ऐप्स के व्यवहार से मेल करने के साथ आ गए हैं।

सबसे पहले, अपने viewWillAppear में:, कीबोर्ड सूचनाओं की सदस्यता तो आप जानते हैं जब कुंजीपटल दिखाने के लिए और छिपाने जाएगा, और प्रणाली आप कीबोर्ड के आकार बता देंगे, लेकिन न 'अपने viewWillDisappear में पंजीकरण रद्द करना भूल जाते हैं :.

[[NSNotificationCenter defaultCenter]
    addObserver:self
       selector:@selector(keyboardWillShow:)
           name:UIKeyboardWillShowNotification
         object:nil];
[[NSNotificationCenter defaultCenter]
    addObserver:self
       selector:@selector(keyboardWillHide:)
           name:UIKeyboardWillHideNotification
         object:nil];

नीचे के समान तरीकों को लागू करें, ताकि आप एक बार कुंजीपटल शो दृश्य क्षेत्र के अनुसार अपने tableView के आकार को समायोजित। यहाँ मैं कुंजीपटल के राज्य पर नज़र रखने कर रहा हूँ अलग से तो मैं जब tableView वापस पूरी ऊंचाई के लिए अपने आप को स्थापित करने के लिए चुन सकते हैं, क्योंकि आपको प्रत्येक क्षेत्र परिवर्तन पर इन सूचनाएं प्राप्त करें। keyboardWillHide को लागू करने के लिए मत भूलना और कहीं अपने tableView आकार तय करने के लिए उपयुक्त हो।

-(void) keyboardWillShow:(NSNotification *)note
{
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
    keyboardHeight = keyboardBounds.size.height;
    if (keyboardIsShowing == NO)
    {
        keyboardIsShowing = YES;
        CGRect frame = self.view.frame;
        frame.size.height -= keyboardHeight;

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:0.3f];
        self.view.frame = frame;
        [UIView commitAnimations];
    }
}

अब यहाँ स्क्रॉल सा है, हमें पहले कुछ आकार बाहर काम करते हैं, तो हम देखते हैं कि हम कहाँ दृश्य क्षेत्र में हैं, और रेक्ट हम करने के लिए स्क्रॉल करना चाहते हैं निर्धारित ऊपर या आधारित पाठ क्षेत्र के बीच नीचे या तो आधा दृश्य होने के लिए जहां यह ध्यान में रखते हुए है पर। इस मामले में, हम UITextFields की एक सरणी और एक enum कि उन पर नज़र रखता है, इसलिए पंक्ति संख्या से rowHeight गुणा हमें वास्तविक इस बाहरी दृश्य के भीतर फ्रेम के ऑफसेट देता है।

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    CGFloat rowHeight = self.tableView.rowHeight;
    if (textField == textFields[CELL_FIELD_ONE])
    {
        frame.origin.y += rowHeight * CELL_FIELD_ONE;
    }
    else if (textField == textFields[CELL_FIELD_TWO])
    {
        frame.origin.y += rowHeight * CELL_FIELD_TWO;
    }
    else if (textField == textFields[CELL_FIELD_THREE])
    {
        frame.origin.y += rowHeight * CELL_FIELD_THREE;
    }
    else if (textField == textFields[CELL_FIELD_FOUR])
    {
        frame.origin.y += rowHeight * CELL_FIELD_FOUR;
    }
    CGFloat viewHeight = self.tableView.frame.size.height;
    CGFloat halfHeight = viewHeight / 2;
    CGFloat midpoint = frame.origin.y + (textField.frame.size.height / 2);
    if (midpoint < halfHeight)
    {
        frame.origin.y = 0;
        frame.size.height = midpoint;
    }
    else
    {
        frame.origin.y = midpoint;
        frame.size.height = midpoint;
    }
    [self.tableView scrollRectToVisible:frame animated:YES];
}

यह काफी अच्छी तरह से काम करने के लिए लगता है।

23/03/2009 को 02:49
का स्रोत उपयोगकर्ता

वोट
22

के लिए सरल समाधान स्विफ्ट :

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar?.becomeFirstResponder()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyViewController.keyboardWillShow(_:)), name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyViewController.keyboardWillHide(_:)), name: UIKeyboardDidHideNotification, object: nil)
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size.height {
            tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    UIView.animateWithDuration(0.2, animations: { self.table_create_issue.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) })
    // For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
    }
25/08/2015 को 06:42
का स्रोत उपयोगकर्ता

वोट
6

मुझे आशा है कि आप लोग पहले से ही एक समाधान के लिए उन सभी को पढ़ने मिला है। इस प्रकार लेकिन मैं अपने समाधान मिल गया। मैं उम्मीद कर रहा हूँ आप पहले से ही के साथ एक सेल है UITextField। तो तैयार करने पर सिर्फ पाठ क्षेत्र के टैग में पंक्ति इंडेक्स तैयार करते हैं।

cell.textField.tag = IndexPath.row;

एक बनाएं activeTextField, के कहने UITextFieldके रूप में नीचे वैश्विक गुंजाइश के साथ:

@interface EditViewController (){

    UITextField *activeTextField;

}

तो, अब तुम सिर्फ अंत में मेरी कोड पेस्ट कॉपी। और यह भी जोड़ने के लिए भूल नहीं हैUITextFieldDelegate

#pragma mark - TextField Delegation

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    activeTextField = textField;

    return YES;
}

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

    activeTextField = nil;

}

रजिस्टर कुंजीपटल notifications

#pragma mark - Keyboard Activity

- (void)registerForKeyboardNotifications

{

    [[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWasShown:)

                                             name:UIKeyboardDidShowNotification object:nil];



    [[NSNotificationCenter defaultCenter] addObserver:self

                                         selector:@selector(keyboardWillBeHidden:)

                                             name:UIKeyboardWillHideNotification object:nil];



}

कीबोर्ड संभालती है Notifications:

जब कहा जाता है UIKeyboardDidShowNotificationभेजा है।

- (void)keyboardWasShown:(NSNotification*)aNotification

{

    NSDictionary* info = [aNotification userInfo];

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

    [self.tableView setContentInset:contentInsets];

    [self.tableView setScrollIndicatorInsets:contentInsets];

    NSIndexPath *currentRowIndex = [NSIndexPath indexPathForRow:activeTextField.tag inSection:0];

    [self.tableView scrollToRowAtIndexPath:currentRowIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

कहा जाता है जब UIKeyboardWillHideNotificationभेज दिया जाता है

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    UIEdgeInsets contentInsets = UIEdgeInsetsZero;

    [self.tableView setContentInset:contentInsets];

    [self.tableView setScrollIndicatorInsets:contentInsets];

}

अब एक बात छोड़ दिया है, कॉल registerForKeyboardNotificationsकरने के लिए विधि ViewDidLoadइस प्रकार विधि:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Registering keyboard notification

    [self registerForKeyboardNotifications];

    // Your codes here...

}

तुम्हें पता है, किया जाता है आशा है कि आपकी textFieldsइच्छा नहीं रह कुंजीपटल से छिपा हुआ।

03/01/2015 को 21:36
का स्रोत उपयोगकर्ता

वोट
6

संयोजन और कई जवाब से रिक्त स्थान को भरने और एक अन्य पोस्ट, इस पोर्ट्रेट या लैंडस्केप मोड में एक iPad पर एसडीके 4.3 के लिए बॉक्स से बाहर काम करेंगे (विशेष रूप से Ortwin Gentz, उपयोगकर्ता 98,013 में):

@implementation UIView (FindFirstResponder)
- (UIResponder *)findFirstResponder
{
  if (self.isFirstResponder) {        
    return self;     
  }

  for (UIView *subView in self.subviews) {
    UIResponder *firstResponder = [subView findFirstResponder];
    if (firstResponder != nil) {
      return firstResponder;
    }
  }

  return nil;
}
@end

@implementation MyViewController

- (UIResponder *)currentFirstResponder {
  return [self.view findFirstResponder];
}

- (IBAction)editingEnded:sender {
  [sender resignFirstResponder];
}

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

- (void)textFieldDidBeginEditing:(UITextField *)textField {
  UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
  [_tableView scrollToRowAtIndexPath:[_tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)keyboardWillShow:(NSNotification*)notification {
  if ([self currentFirstResponder] != nil) {
    NSDictionary* userInfo = [notification userInfo];

    // we don't use SDK constants here to be universally compatible with all SDKs ≥ 3.0
    NSValue* keyboardFrameValue = [userInfo objectForKey:@"UIKeyboardBoundsUserInfoKey"];
    if (!keyboardFrameValue) {
      keyboardFrameValue = [userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"];
    }

    // Reduce the tableView height by the part of the keyboard that actually covers the tableView
    CGRect windowRect = [[UIApplication sharedApplication] keyWindow].bounds;
    CGRect viewRectAbsolute = [_tableView convertRect:_tableView.bounds toView:[[UIApplication sharedApplication] keyWindow]];
    CGRect frame = _tableView.frame;
    if (UIInterfaceOrientationLandscapeLeft == self.interfaceOrientation ||UIInterfaceOrientationLandscapeRight == self.interfaceOrientation ) {
      windowRect = CGRectMake(windowRect.origin.y, windowRect.origin.x, windowRect.size.height, windowRect.size.width);
      viewRectAbsolute = CGRectMake(viewRectAbsolute.origin.y, viewRectAbsolute.origin.x, viewRectAbsolute.size.height, viewRectAbsolute.size.width);
    }
    frame.size.height -= [keyboardFrameValue CGRectValue].size.height - CGRectGetMaxY(windowRect) + CGRectGetMaxY(viewRectAbsolute);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    _tableView.frame = frame;
    [UIView commitAnimations];

    UITableViewCell *textFieldCell = (id)((UITextField *)self.currentFirstResponder).superview.superview;
    NSIndexPath *textFieldIndexPath = [_tableView indexPathForCell:textFieldCell];

    // iOS 3 sends hide and show notifications right after each other
    // when switching between textFields, so cancel -scrollToOldPosition requests
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    _topmostRowBeforeKeyboardWasShown = [[_tableView indexPathsForVisibleRows] objectAtIndex:0];
    [_tableView scrollToRowAtIndexPath:textFieldIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  }
}

- (void) scrollToOldPosition {
  [_tableView scrollToRowAtIndexPath:_topmostRowBeforeKeyboardWasShown atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)keyboardWillHide:(NSNotification*)notification {
  if ([self currentFirstResponder] != nil) {

    NSDictionary* userInfo = [notification userInfo];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
    [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
    _tableView.frame = self.view.bounds;
    [UIView commitAnimations];

    [self performSelector:@selector(scrollToOldPosition) withObject:nil afterDelay:0.1];
  }
}   

@end
03/08/2011 को 03:35
का स्रोत उपयोगकर्ता

वोट
5

मेरे दृष्टिकोण:

मैं पहली बार UITextField उपवर्ग और एक indexPath संपत्ति जोड़ें। cellFor में ... विधि मैं indexPath संपत्ति सौंपने।

तब मैं कोड निम्नलिखित जोड़ें:

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:textField.indexPath];

CGPoint cellPoint = [cell convertPoint:textField.center toView:self.tableView];
[UIView animateWithDuration:0.3 animations:^(void){self.tableView.contentOffset = CGPointMake(0, cellPoint.y-50);}];

textFieldShould / WillBegin ... आदि के लिए।

जब कीबोर्ड गायब हो जाता है उसे अपने साथ उल्टा करने के लिए है:

[UIView animateWithDuration:0.3 animations:^(void){self.tableView.contentOffset = CGPointMake(0, 0);}];
29/09/2012 को 13:03
का स्रोत उपयोगकर्ता

वोट
4

का प्रयोग करें UITextField's delegateविधि:

तीव्र

func textFieldShouldBeginEditing(textField: UITextField) -> bool {
  let txtFieldPosition = textField.convertPoint(textField.bounds.origin, toView: yourTableViewHere)
  let indexPath = yourTablViewHere.indexPathForRowAtPoint(txtFieldPosition)
  if indexPath != nil {
     yourTablViewHere.scrollToRowAtIndexPath(indexPath!, atScrollPosition: .Top, animated: true)
  }
  return true
}

उद्देश्य सी

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
  CGPoint txtFieldPosition = [textField convertPoint:CGPointZero toView: yourTablViewHere];
  NSLog(@"Begin txtFieldPosition : %@",NSStringFromCGPoint(txtFieldPosition));
  NSIndexPath *indexPath = [yourTablViewHere indexPathForRowAtPoint:txtFieldPosition];

  if (indexPath != nil) {
     [yourTablViewHere scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  }
  return YES;
}
20/03/2015 को 07:00
का स्रोत उपयोगकर्ता

वोट
4

सही जवाब सैम हो की जवाब है:

"आप UIViewController के बजाय UITableViewController का उपयोग करते हैं, यह स्वतः ही ऐसा करेंगे।"।

बस UITableViewController के tableview संपत्ति (ताकि जैसे UITableViewController की संपत्ति देखें की एक subview के रूप में जोड़ना नहीं है) करने के लिए अपने UITableView कनेक्ट करने के लिए सुनिश्चित करें।

इसके अलावा FlexibleHeight करने के लिए अपने UITableView की AutoresizingMask संपत्ति तैयार करना न भूलें

09/12/2010 को 11:28
का स्रोत उपयोगकर्ता

वोट
4

आप का उपयोग करते हैं Three20, तो का उपयोग autoresizesForKeyboardसंपत्ति। बस अपने दृश्य नियंत्रक के दशक में सेट -initWithNibName:bundleविधि

self.autoresizesForKeyboard = YES

इस का ख्याल रखता है:

  1. कुंजीपटल सूचनाओं के लिए सुनकर और तालिका दृश्य के फ्रेम का समायोजन
  2. पहले प्रत्युत्तर करने के लिए स्क्रॉल

किया और किया।

21/09/2010 को 14:19
का स्रोत उपयोगकर्ता

वोट
4

कीबोर्ड सूचनाएं काम करते हैं, लेकिन उस के लिए एप्पल के नमूना कोड मानता है कि पुस्तक दृश्य खिड़की की जड़ दृश्य है। यह आमतौर पर ऐसा नहीं है। आप टैब सलाखों, आदि के लिए क्षतिपूर्ति करने, ऑफसेट सही पाने के लिए किया है।

यह तुलना में आसान लग रहा है। यहाँ कोड मैं एक UITableViewController में इस्तेमाल करते हैं। यह दो उदाहरण चर, hiddenRect और keyboardShown है।

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification {
    if (keyboardShown)
        return;

    NSDictionary* info = [aNotification userInfo];

    // Get the frame of the keyboard.
    NSValue *centerValue = [info objectForKey:UIKeyboardCenterEndUserInfoKey];
    NSValue *boundsValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGPoint keyboardCenter = [centerValue CGPointValue];
    CGRect keyboardBounds = [boundsValue CGRectValue];
    CGPoint keyboardOrigin = CGPointMake(keyboardCenter.x - keyboardBounds.size.width / 2.0,
                                         keyboardCenter.y - keyboardBounds.size.height / 2.0);
    CGRect keyboardScreenFrame = { keyboardOrigin, keyboardBounds.size };


    // Resize the scroll view.
    UIScrollView *scrollView = (UIScrollView *) self.tableView;
    CGRect viewFrame = scrollView.frame;
    CGRect keyboardFrame = [scrollView.superview convertRect:keyboardScreenFrame fromView:nil];
    hiddenRect = CGRectIntersection(viewFrame, keyboardFrame);

    CGRect remainder, slice;
    CGRectDivide(viewFrame, &slice, &remainder, CGRectGetHeight(hiddenRect), CGRectMaxYEdge);
    scrollView.frame = remainder;

    // Scroll the active text field into view.
    CGRect textFieldRect = [/* selected cell */ frame];
    [scrollView scrollRectToVisible:textFieldRect animated:YES];

    keyboardShown = YES;
}


// Called when the UIKeyboardDidHideNotification is sent
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
    // Reset the height of the scroll view to its original value
    UIScrollView *scrollView = (UIScrollView *) self.tableView;
    CGRect viewFrame = [scrollView frame];
    scrollView.frame = CGRectUnion(viewFrame, hiddenRect);

    keyboardShown = NO;
}
11/07/2009 को 23:01
का स्रोत उपयोगकर्ता

वोट
4

यदि आप एक UITableView का उपयोग करते हैं (अपने textfields जगह जेफ लेमार्क से ), तो आप सिर्फ इसलिए की तरह प्रतिनिधि विधि का उपयोग कर tableview स्क्रॉल कर सकते हैं।

(नोट: अपने पाठ क्षेत्रों में एक ही सूचकांक के साथ एक सरणी में जमा हो जाती है क्योंकि tableview में पंक्ति)

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

        int index;
        for(UITextField *aField in textFields){

            if (textField == aField){
                index = [textFields indexOfObject:aField]-1;
            }
        }

         if(index >= 0) 
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

        [super textFieldDidBeginEditing:textField];
    }
01/05/2009 को 07:09
का स्रोत उपयोगकर्ता

वोट
3

एक और अधिक स्ट्रीम लाइन समाधान। यह UITextField प्रतिनिधि तरीकों में निकल जाता है, तो यह w / UIKeyboard सूचनाएं खिलवाड़ आवश्यकता नहीं है।

कार्यान्वयन नोट्स:

kSettingsRowHeight - एक UITableViewCell की ऊंचाई।

offsetTarget और offsetThreshold kSettingsRowHeight के बंद baed कर रहे हैं। आप एक अलग पंक्ति की ऊँचाई का उपयोग करते हैं, तो बिंदु का y संपत्ति के लिए उन मूल्यों को निर्धारित किया है। [Alt: पंक्ति एक अलग ढंग से ऑफसेट की गणना।]

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGFloat offsetTarget    = 113.0f; // 3rd row
CGFloat offsetThreshold = 248.0f; // 6th row (i.e. 2nd-to-last row)

CGPoint point = [self.tableView convertPoint:CGPointZero fromView:textField];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

CGRect frame = self.tableView.frame;
if (point.y > offsetThreshold) {
    self.tableView.frame = CGRectMake(0.0f,
                      offsetTarget - point.y + kSettingsRowHeight,
                      frame.size.width,
                      frame.size.height);
} else if (point.y > offsetTarget) {
    self.tableView.frame = CGRectMake(0.0f,
                      offsetTarget - point.y,
                      frame.size.width,
                      frame.size.height);
} else {
    self.tableView.frame = CGRectMake(0.0f,
                      0.0f,
                      frame.size.width,
                      frame.size.height);
}

[UIView commitAnimations];

return YES;

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

CGRect frame = self.tableView.frame;
self.tableView.frame = CGRectMake(0.0f,
                  0.0f,
                  frame.size.width,
                  frame.size.height);

[UIView commitAnimations];

return YES;

}

04/08/2009 को 08:18
का स्रोत उपयोगकर्ता

वोट
3

मैं आपकी समस्या (मैं एक स्क्रीन दूसरे के ऊपर पर खड़ी संपादन योग्य कोशिकाओं के एक समूह के साथ iPhone की settings.app के समान चाहता था) और पाया कि इस दृष्टिकोण अच्छी तरह से काम की तरह कुछ में भाग:

से बचने के लिए चारों ओर uitextfields रपट

27/02/2009 को 15:17
का स्रोत उपयोगकर्ता

वोट
2

स्विफ्ट में एक उदाहरण से पाठ क्षेत्र के सटीक बिंदु का उपयोग कर प्राप्त indexPath स्विफ्ट के साथ UITableViewCell में UITextField की :

func textFieldDidBeginEditing(textField: UITextField) {
    let pointInTable = textField.convertPoint(textField.bounds.origin, toView: self.accountsTableView)
    let textFieldIndexPath = self.accountsTableView.indexPathForRowAtPoint(pointInTable)
    accountsTableView.scrollToRowAtIndexPath(textFieldIndexPath!, atScrollPosition: .Top, animated: true)
}
21/05/2015 को 06:34
का स्रोत उपयोगकर्ता

वोट
2

बहुत दिलचस्प चर्चा धागा, मैं भी एक ही समस्या का सामना करना पड़ा बदतर एक कारण हो सकता है

  1. मैं एक कस्टम सेल का उपयोग किया गया था और पाठ फ़ील्ड कि अंदर था।
  2. मैं UIViewController उपयोग करने के लिए मेरी आवश्यकताओं को पूरा करने तो नहीं कर सकते UITableViewController का लाभ लेने के लिए किया था।
  3. मैं अपने तालिका सेल में फिल्टर / क्रमबद्ध criterias था, यानी उर कोशिकाओं बदल रहा है और indexpath का ट्रैक रखने रहती है और सभी में मदद नहीं करेगा।

तो यहाँ धागे पढ़ सकते हैं और मेरी संस्करण है, जो मुझ में आईपैड में मेरी सामग्री को धक्का करने में मदद की कार्यान्वित परिदृश्य मोड। यहाँ कोड है (यह सबूत और सभी मूर्ख नहीं है, लेकिन यह मेरी समस्या का समाधान हो) सबसे पहले यू अपने कस्टम सेल वर्ग है, जो संपादन को शुरू होता है में एक प्रतिनिधि की आवश्यकता है, उर ViewController के लिए पाठ फ़ील्ड भेजता है और वहाँ activefield = theTextField सेट

// केवल लैंडस्केप मोड संभाल करने के लिए लागू

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbValue = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect aRect = myTable.frame;

    CGSize kbSize = CGSizeMake(kbValue.height, kbValue.width);

    aRect.size.height -= kbSize.height+50;
// This will the exact rect in which your textfield is present
        CGRect rect =  [myTable convertRect:activeField.bounds fromView:activeField];
// Scroll up only if required
    if (!CGRectContainsPoint(aRect, rect.origin) ) {


            [myTable setContentOffset:CGPointMake(0.0, rect.origin.y) animated:YES];

    }


}

// जब UIKeyboardWillHideNotification भेज दिया जाता है कहा जाता है

- (void)keyboardWillHide:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    myTable.contentInset = contentInsets;
    myTable.scrollIndicatorInsets = contentInsets;
    NSDictionary* info = [aNotification userInfo];
    CGSize kbValue = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGSize kbSize = CGSizeMake(kbValue.height, kbValue.width);
    CGRect bkgndRect = activeField.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [activeField.superview setFrame:bkgndRect];
    [myTable setContentOffset:CGPointMake(0.0, 10.0) animated:YES];
}

-anoop4real

17/07/2012 को 18:11
का स्रोत उपयोगकर्ता

वोट
2

यह soluton मेरे लिए काम करता, कृपया ध्यान दें लाइन

[tableView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height+160) animated:YES];

आप इसे आप के साथ काम मैच के लिए 160 मूल्य बदल सकते हैं

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = activeField.superview.frame;
                        bkgndRect.size.height += kbSize.height;
     [activeField.superview setFrame:bkgndRect];
     [tableView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height+160) animated:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
   activeField = textField;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
 {
     activeField = nil;
 }
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    tableView.contentInset = contentInsets;
    tableView.scrollIndicatorInsets = contentInsets;
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = activeField.superview.frame;
    //bkgndRect.size.height += kbSize.height;
    [activeField.superview setFrame:bkgndRect];
    [tableView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
}
02/12/2011 को 19:28
का स्रोत उपयोगकर्ता

वोट
2

चूंकि आप एक तालिका में textfields है, सबसे अच्छा तरीका है वास्तव में तालिका का आकार बदलने के लिए है - आप कुंजीपटल के आकार से (मैं चारों ओर 165 पिक्सल लगता है) फिर जब tableView.frame सेट ऊंचाई में छोटा होना और उसके बाद का विस्तार करने की जरूरत कुंजीपटल को खारिज कर दिया है।

तुम भी tableView के लिए अक्षम उपयोगकर्ता बातचीत उस समय के साथ-साथ वैकल्पिक रूप से कर सकते हैं, यदि आप उपयोगकर्ता स्क्रॉल नहीं करना चाहती।

28/02/2009 को 19:37
का स्रोत उपयोगकर्ता

वोट
1

साथ छोटा सा बदलाव स्विफ्ट 4.2 ...

मेरी UITableView पर मैं कई वर्गों था, लेकिन मैं करना पड़ा चल हैडर प्रभाव से बचने तो मैं एक "का इस्तेमाल किया dummyViewHeight " दृष्टिकोण के रूप में कहीं और स्टैक ओवरफ़्लो पर यहाँ देखा ... तो यह इस समस्या के लिए मेरे समाधान (यह कीबोर्ड के लिए भी काम करता है + उपकरण पट्टी + सुझाव):

वर्ग निरंतर रूप में यह घोषित:

let dummyViewHeight: CGFloat = 40.0

फिर

override func viewDidLoad() {
    super.viewDidLoad()
    //... some stuff here, not needed for this example

    // Create non floating header
    tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: dummyViewHeight))
    tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: 0, right: 0)

    addObservers()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    removeObservers()
}

और यहाँ सब जादू ...

@objc func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        let keyboardHeight = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size.height
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
        tableView.contentInset = UIEdgeInsets(top: -dummyViewHeight, left: 0, bottom: keyboardHeight, right: 0)
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    UIView.animate(withDuration: 0.25) {
        self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: self.dummyViewHeight))
        self.tableView.contentInset = UIEdgeInsets(top: -self.dummyViewHeight, left: 0, bottom: 0, right: 0)
    }
}
08/10/2018 को 10:45
का स्रोत उपयोगकर्ता

वोट
1

viewDidLoad में

-(void)viewdidload{

[super viewdidload];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

    -(void)keyboardWillChange:(NSNotification*)sender{

        NSLog(@"keyboardwillchange sender %@",sender);

float margin=0  // set your own topmargin


        CGFloat originY = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;


        if (originY >= self.view.frame.size.height){

            NSLog(@"keyboardclose");



            [tb_ setFrame:CGRectMake(0, margin, self.view.frame.size.width, self.view.frame.size.height-margin)];

        }else{

            NSLog(@"keyobard on");

            float adjustedHeight = self.view.frame.size.height - margin - (self.view.frame.size.height-originY);

            [tb_ setFrame:CGRectMake(0, margin, self.view.frame.size.width, adjustedHeight)];
        }







    }
12/02/2016 को 09:14
का स्रोत उपयोगकर्ता

वोट
1

मैं इन का उपयोग कर रहा है और वे एक आकर्षण की तरह काम करते हैं:

BSKeyboardControls - BSKeyboardControls GitHub

TPKeyboardAvoiding - TPKeyboardAvoiding GitHub

13/02/2014 को 09:30
का स्रोत उपयोगकर्ता

वोट
1

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

यह जाँच करें यहाँ

12/02/2014 को 21:27
का स्रोत उपयोगकर्ता

वोट
1

मैं अपने समाधान (या QuickDialog की है कि) टोपी में फेंक देते हैं। मूल रूप से स्क्रॉल करने के लिए चेतन करने के लिए प्रतीक्षा करें। यह जादुई संख्या के बजाय कुंजीपटल एनीमेशन JIT प्राप्त करने के लिए अच्छा होगा।

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.emailTextField) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 50 * USEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        });
    }
}
28/01/2014 को 19:05
का स्रोत उपयोगकर्ता

वोट
1

आसान और तेजी से समाधान।

मैं सिर्फ सही सेल करने के लिए स्क्रॉल जब भी स्क्रॉल होता है

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView 

मान लिया जाये कि मैं अब तालिका जानते हैं कि इस मोड "_keepMyCellOnTop" और मैं चयनित सेल पता है "_selectedCellIndex" में है या चयनित सेल करने के लिए स्क्रॉल

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{

    if (_keepMyCellOnTop)
    {
        [self.tableView scrollToRowAtIndexPath:_selectedCellIndex atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
}

यह स्क्रॉल कर पाएगा।

में कोड लगाकर -(void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView एक स्क्रॉल ऊपर और नीचे परिणाम होगा

31/12/2013 को 13:37
का स्रोत उपयोगकर्ता

वोट
1

मैं बस के बाद मैं गूगल और स्टैक ओवरफ़्लो के माध्यम से पाया समाधान के एक बड़े पैमाने पर संदर्भित किया अपने आप से ऐसी समस्या को हल किया है।

सबसे पहले, विश्वास दिलाता हूं कृपया है कि आप अपने UIScrollView के एक IBOutlet की स्थापना की है, तब से विशेष ध्यान दें कृपया एप्पल डॉक्टर: कीबोर्ड प्रबंधन । अंत में, आप पृष्ठभूमि स्क्रॉल कर सकते हैं, लेकिन कुंजीपटल अभी भी पाठ फ़ील्ड को शामिल किया गया है, तो कृपया कोड के इस टुकड़े पर एक नजर है:

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;

if (aRect.size.height < activeField.frame.origin.y+activeField.frame.size.height) {

    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y+activeField.frame.size.height-aRect.size.height);

    [scrollView setContentOffset:scrollPoint animated:YES];

इस टुकड़े और एप्पल के अगर हालत में झूठ के बीच मुख्य अंतर। मेरा मानना ​​है कि पुस्तक दूरी और का है कि क्या पाठ कीबोर्ड के अंतर्गत आने वाले क्षेत्र में सही नहीं हैं हालत के सेब की गणना, तो मैं के रूप में ऊपर मेरी संशोधन कर दिया।

मुझे पता है अगर यह काम करता है

18/08/2012 को 11:10
का स्रोत उपयोगकर्ता

वोट
1

यहाँ कैसे मैं इस काम है, जो सैम हो और मार्सेल डब्ल्यू के जवाब है, और अपने ही बग मेरी भद्दा कोड में किए गए सुधारों से कुछ का एक मिश्रण है बनाया है। मैं एक UITableViewController उपयोग कर रहा था। तालिका अब सही तरीके से आकार बदलता है जब कीबोर्ड दिखाया गया है।

1) में viewDidLoadमैं कहा:

self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

2) मैं फोन करने के लिए भूल गया था superमें समकक्ष viewWillAppearऔर awakeFromNib। मैं में इन वापस शामिल किया।

26/07/2012 को 18:18
का स्रोत उपयोगकर्ता

वोट
1

अपने UITableView UITableViewController और नहीं UITableView का एक उपवर्ग द्वारा किया जाता है, और पाठ क्षेत्र प्रतिनिधि UITableViewController है, तो इसे का प्रबंधन करना चाहिए सब स्वचालित रूप से स्क्रॉल - इन सभी अन्य टिप्पणी व्यवहार में लागू करने के लिए बहुत मुश्किल हो जाता।

एक अच्छा उदाहरण के लिए देखने के सेब उदाहरण कोड परियोजना: TaggedLocations।

आप देख सकते हैं कि यह स्वचालित रूप से स्क्रॉल, लेकिन वहाँ किसी भी कोड है कि इस करता है होना करने के लिए प्रतीत नहीं होता। इस परियोजना में कस्टम तालिका दृश्य कोशिकाएं होती हैं, इसलिए यदि आप एक गाइड के रूप में यह के साथ अपने आवेदन का निर्माण, आप इच्छित परिणाम प्राप्त करना चाहिए।

05/03/2012 को 07:09
का स्रोत उपयोगकर्ता

वोट
1

एक और आसान तरीका (केवल एक अनुभाग के साथ काम करता है)

//cellForRowAtIndexPath
UItextField *tf;
[cell addSubview:tf];
tf.tag = indexPath.row;
tf.delegate = self;

//textFieldDidBeginEditing:(UITextField *)text
[[self.tableView scrollToRowsAtIndexPath:[NSIndexPath indexPathForRow:text.tag in section:SECTIONINTEGER] animated:YES];
23/11/2011 को 17:25
का स्रोत उपयोगकर्ता

वोट
1

तो बाद भीषण काम के घंटे (नाकाम रहने और पूरी तरह से) इन वर्तमान समाधान का उपयोग करने की कोशिश कर रहा अंत में अच्छी तरह से काम कर रहे चीजें मिल गया है, और उन्हें अद्यतन नई एनीमेशन ब्लॉक का उपयोग करने के लिए। मेरा जवाब पूरी तरह से पर आधारित है ऊपर Ortwin का जवाब

तो जो भी कारण के लिए कोड के ऊपर सिर्फ मेरे लिए काम नहीं कर रहा था। मेरे सेटअप काफी दूसरों के समान लग रहा था, लेकिन शायद इसलिए क्योंकि मैं एक iPad या 4.3 ... पता नहीं पर था। यह कुछ निराला गणित कर रहा था और स्क्रीन बंद मेरी tableview शूटिंग।

: मेरे समाधान के अंतिम परिणाम देखें http://screencast.com/t/hjBCuRrPC (कृपया तस्वीर उपेक्षा :-P।)

इसलिए मैं Ortwin क्या कर रहा था का सार के साथ चला गया, लेकिन बदल कि यह कैसे कुछ गणित कर रहा था कीबोर्ड की ऊंचाई के साथ origin.y और मेरी तालिका दृश्य की size.height को जोड़ने के लिए। जब मैं उस परिणाम से खिड़की की ऊंचाई घटाना, यह मेरे बताता है कि कितना चौराहे मैं चल रहा है। इसकी 0 से अधिक (उर्फ वहाँ कुछ ओवरलैप है) अगर मैं फ्रेम ऊंचाई की एनीमेशन प्रदर्शन करते हैं।

इसके अलावा कुछ मुद्दों है कि 1 से हल किया गया) सेल करने के लिए स्क्रॉल करने के लिए जब तक एनीमेशन किया गया था प्रतीक्षा कर रहा है और 2) UIViewAnimationOptionBeginFromCurrentState विकल्प का उपयोग कर जब कुंजीपटल छुपा पुनः बनाने थे।

कुछ बातें ध्यान दें।

  • _topmostRowBeforeKeyboardWasShown और _originalFrame उदाहरण चर शीर्षक में घोषित कर रहे हैं।
  • self.guestEntryTableView मेरी tableView (मैं एक बाहरी फ़ाइल में हूँ) है
  • IASKCGRectSwap एक फ्रेम के निर्देशांक flipping के लिए Ortwin की विधि है
  • मैं केवल tableView की ऊंचाई को अद्यतन करता है, तो यह कम से कम 50px दिखा होने जा रही है
  • जब से मैं एक UIViewController में नहीं हूँ मैं self.view नहीं है, तो मैं बस अपने मूल फ्रेम करने के लिए tableView वापसी

फिर से, मैं इस सवाल का जवाब मिल गया के पास नहीं होता है, तो मैं Ortwin यह की जड़ नहीं प्रदान की थी। कोड यह रहा:

- (IBAction)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeTextField = textField;

    if ([self.guestEntryTableView indexPathsForVisibleRows].count) {
        _topmostRowBeforeKeyboardWasShown = (NSIndexPath*)[[self.guestEntryTableView indexPathsForVisibleRows] objectAtIndex:0];
    } else {
        // this should never happen
        _topmostRowBeforeKeyboardWasShown = [NSIndexPath indexPathForRow:0 inSection:0];
        [textField resignFirstResponder];
    }
}

- (IBAction)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeTextField = nil;
}

- (void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary* userInfo = [notification userInfo];

    NSValue* keyboardFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    // Reduce the tableView height by the part of the keyboard that actually covers the tableView
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGRect windowRect = [[UIApplication sharedApplication] keyWindow].bounds;
    CGRect viewRectAbsolute = [self.guestEntryTableView convertRect:self.guestEntryTableView.bounds toView:[[UIApplication sharedApplication] keyWindow]];
    CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
    if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) {
        windowRect = IASKCGRectSwap(windowRect);
        viewRectAbsolute = IASKCGRectSwap(viewRectAbsolute);
        keyboardFrame = IASKCGRectSwap(keyboardFrame);
    }

    // fix the coordinates of our rect to have a top left origin 0,0
    viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height);

    CGRect frame = self.guestEntryTableView.frame;
    _originalFrame = self.guestEntryTableView.frame;

    int remainder = (viewRectAbsolute.origin.y + viewRectAbsolute.size.height + keyboardFrame.size.height) - windowRect.size.height;

    if (remainder > 0 && !(remainder > frame.size.height + 50)) {
        frame.size.height = frame.size.height - remainder;
        float duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        [UIView animateWithDuration: duration
                        animations:^{
                            self.guestEntryTableView.frame = frame;
                        }
                        completion:^(BOOL finished){
                            UITableViewCell *textFieldCell = (UITableViewCell*) [[self.activeTextField superview] superview];
                            NSIndexPath *textFieldIndexPath = [self.guestEntryTableView indexPathForCell:textFieldCell];
                            [self.guestEntryTableView scrollToRowAtIndexPath:textFieldIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
                        }];
    }

}

- (void)keyboardWillHide:(NSNotification*)notification {
    NSDictionary* userInfo = [notification userInfo];
    float duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration: duration
                          delay: 0.0
                        options: (UIViewAnimationOptionBeginFromCurrentState)
                     animations:^{
                         self.guestEntryTableView.frame = _originalFrame;
                     }
                     completion:^(BOOL finished){
                         [self.guestEntryTableView scrollToRowAtIndexPath:_topmostRowBeforeKeyboardWasShown atScrollPosition:UITableViewScrollPositionTop animated:YES];
                     }];

}   

#pragma mark CGRect Utility function
CGRect IASKCGRectSwap(CGRect rect) {
    CGRect newRect;
    newRect.origin.x = rect.origin.y;
    newRect.origin.y = rect.origin.x;
    newRect.size.width = rect.size.height;
    newRect.size.height = rect.size.width;
    return newRect;
}

CGRect FixOriginRotation(CGRect rect, UIInterfaceOrientation orientation, int parentWidth, int parentHeight) {
    CGRect newRect;
    switch(orientation)
    {
        case UIInterfaceOrientationLandscapeLeft:
            newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), rect.origin.y, rect.size.width, rect.size.height);
            break;
        case UIInterfaceOrientationLandscapeRight:
            newRect = CGRectMake(rect.origin.x, parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
            break;
        case UIInterfaceOrientationPortrait:
            newRect = rect;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
            break;
    }
    return newRect;
}
18/07/2011 को 09:45
का स्रोत उपयोगकर्ता

वोट
1

मैं लगभग एक ही दृष्टिकोण की कोशिश की और उसी के लिए एक सरल और छोटे कोड के साथ आया था। मैं एक IBOutlet iTextView बनाया है और आईबी में UITextView के साथ जुड़े।

 -(void)keyboardWillShow:(NSNotification *)notification
    {
        NSLog(@"Keyboard");
        CGRect keyFrame = [[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

        [UIView beginAnimations:@"resize view" context:nil];
        [UIView setAnimationCurve:1];
        [UIView setAnimationDuration:1.0];
        CGRect frame = iTableView.frame;
        frame.size.height = frame.size.height -  keyFrame.size.height;
        iTableView.frame = frame;
        [iTableView scrollRectToVisible:frame animated:YES];
        [UIView commitAnimations];

    }
13/05/2011 को 06:00
का स्रोत उपयोगकर्ता

वोट
1

यह पूरी तरह से काम करता है, और भी आईपैड पर।

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{

    if(textField == textfield1){
            [accountName1TextField becomeFirstResponder];
        }else if(textField == textfield2){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield3 becomeFirstResponder];

        }else if(textField == textfield3){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield4 becomeFirstResponder];

        }else if(textField == textfield4){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield5 becomeFirstResponder];

        }else if(textField == textfield5){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield6 becomeFirstResponder];

        }else if(textField == textfield6){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield7 becomeFirstResponder];

        }else if(textField == textfield7){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield8 becomeFirstResponder];

        }else if(textField == textfield8){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textfield9 becomeFirstResponder];

        }else if(textField == textfield9){
            [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:7 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [textField resignFirstResponder];
        }
23/10/2010 को 08:11
का स्रोत उपयोगकर्ता

वोट
0

मैं सिर्फ एक और बग जब UITableViewController का उपयोग कर की खोज की। यह स्वचालित रूप से स्क्रॉल नहीं था जब कीबोर्ड दिखाया। मैंने देखा है कि यह contentInsetAdjustmentBehavior = UITableView पर .never की वजह से था।

03/07/2019 को 21:30
का स्रोत उपयोगकर्ता

वोट
0

एनिमेशन और कुंजीपटल फ्रेम बदलते साथ स्विफ्ट 3-4 के लिए समाधान:

सबसे पहले, एक बूल बनाने के लिए:

// MARK: - Private Properties
private var isKeyboardShowing = false

दूसरे, सिस्टम कीबोर्ड सूचनाओं पर पर्यवेक्षकों जोड़ें:

// MARK: - Overriding ViewController Life Cycle Methods
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: .UIKeyboardWillChangeFrame, object: nil)
}

तीसरे, एनीमेशन समारोह को तैयार:

func adjustTableViewInsets(keyboardHeight: CGFloat, duration: NSNumber, curve: NSNumber){
    var extraHeight: CGFloat = 0
    if keyboardHeight > 0 {
        extraHeight = 20
        isKeyboardShowing = true
    } else {
        isKeyboardShowing = false
    }

    let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight + extraHeight, right: 0)
    func animateFunc() {
        //refresh constraints
        //self.view.layoutSubviews()
        tableView.contentInset = contentInset
    }

    UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: [UIViewAnimationOptions(rawValue: UInt(curve))], animations: animateFunc, completion: nil)
}

तो फिर लक्ष्य / कार्रवाई विधियों को जोड़ने (पर्यवेक्षकों द्वारा कहा जाता है):

// MARK: - Target/Selector Actions
func keyboardWillShow(notification: NSNotification) {
    if !isKeyboardShowing {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = keyboardSize.height

            let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
            let curve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber

            adjustTableViewInsets(keyboardHeight: keyboardHeight, duration: duration, curve: curve)
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
    let curve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber
    adjustTableViewInsets(keyboardHeight: 0, duration: duration, curve: curve)
}

func keyboardWillChangeFrame(notification: NSNotification) {
    if isKeyboardShowing {
        let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
        let curve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber

        if let newKeyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = newKeyboardSize.height
            adjustTableViewInsets(keyboardHeight: keyboardHeight, duration: duration, curve: curve)
        }
    }
}

अन्त में, deinit में या viewWillDisappear में पर्यवेक्षकों को दूर करने के लिए मत भूलना:

deinit {
    NotificationCenter.default.removeObserver(self)
}
10/06/2018 को 15:48
का स्रोत उपयोगकर्ता

वोट
0

कोई आवश्यकता किसी भी गणना, नीचे दिए गए कोड का प्रयोग करें यह काम करेगा: इस कोड को मैं अपने स्वनिर्धारित UITableViewCell में इस्तेमाल किया, यह काम कर रहा है:

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)}


func keyboardWillShow(_ notification:Notification) {

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
}}


func keyboardWillHide(_ notification:Notification) {

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}}
22/02/2018 को 07:47
का स्रोत उपयोगकर्ता

वोट
0

स्विफ्ट 4 पूर्ण समाधान:

  • सही ढंग से कुंजीपटल फ्रेम परिवर्तन (जैसे कीबोर्ड ऊंचाई emojii → सामान्य कीबोर्ड की तरह बदल जाता है) के साथ काम करता है।
  • UITableView उदाहरण के लिए TabBar और Toolbar Support (अन्य उदाहरण में आप गलत सन्निवेश प्राप्त)।
  • गतिशील एनीमेशन अवधि (नहीं हार्ड-कोडेड)।
  • प्रोटोकॉल उन्मुख, ताकि आप आसानी से किसी भी स्थिति में इसका इस्तेमाल कर सकते हैं।
  • स्क्रॉल सन्निवेश भी काम करता है।

मैं सहायक प्रोटोकॉल लिखा था (आप के रूप में यह डाउनलोड कर सकते हैं सार है, क्योंकि यह करने के लिए StackOverflow पर पोस्ट बहुत बड़ी है), आपके विचार तो बस की जरूरत है:

  1. अपनाने KeyboardChangeFrameObserverप्रोटोकॉल:

    func willChangeKeyboardFrame(height: CGFloat, animationDuration: TimeInterval, animationOptions: UIViewAnimationOptions)
    
  2. कॉल observeKeyboardFrameChanges()दिखाई पर।

tableView के लिए है कि प्रोटोकॉल का उदाहरण कार्यान्वयन:

class TestViewController: UITableViewController, KeyboardChangeFrameObserver {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        observeKeyboardFrameChanges()
    }

    func willChangeKeyboardFrame(height: CGFloat, animationDuration: TimeInterval, animationOptions: UIViewAnimationOptions) {
        var adjustedHeight = height

        if let tabBarHeight = self.tabBarController?.tabBar.frame.height {
            adjustedHeight -= tabBarHeight
        } else if let toolbarHeight = navigationController?.toolbar.frame.height, navigationController?.isToolbarHidden == false {
            adjustedHeight -= toolbarHeight
        }

        if adjustedHeight < 0 { adjustedHeight = 0 }

        UIView.animate(withDuration: animationDuration, animations: {
            let newInsets = UIEdgeInsets(top: 0, left: 0, bottom: adjustedHeight, right: 0)
            self.tableView.contentInset = newInsets
            self.tableView.scrollIndicatorInsets = newInsets
        })
    }

}
12/01/2018 को 00:10
का स्रोत उपयोगकर्ता

वोट
0
// scroll tableview so content ends at the middle of the tableview (out of the way of the keyboard)
CGPoint newContentOffset = CGPointMake(0, [self.tableView contentSize].height - (self.tableView.bounds.size.height / 2));
[self.tableView setContentOffset:newContentOffset animated:YES];
27/06/2017 को 21:12
का स्रोत उपयोगकर्ता

वोट
0

मेरी संस्करण को देखो :)

    - (void)keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = cellSelected.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [cellSelected.superview setFrame:bkgndRect];
    [tableView setContentOffset:CGPointMake(0.0, cellSelected.frame.origin.y-kbSize.height) animated:YES];
}


- (void)keyboardWasHidden:(NSNotification *)aNotification
{
    [tableView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
}
02/07/2016 को 20:32
का स्रोत उपयोगकर्ता

वोट
0

यहाँ मेरी समाधान iOS7 कैलेंडर ऐप से "घटना संपादित करें" स्क्रीन से प्रेरित है।

इस समाधान के मुख्य बिंदुओं में से एक है कि कुंजीपटल खारिज किया जाता है जब उपयोगकर्ता स्क्रॉल टेबल है।

कार्यान्वयन:

1) संपत्ति है कि चयनित पाठ फ़ील्ड स्टोर करेगा जोड़ें:

@property (strong) UITextField *currentTextField;

और BOOL चर है कि हम अगर हम कुंजीपटल जब उपयोगकर्ता स्क्रॉल तालिका को छिपाने के लिए की जरूरत है की जाँच करने के प्रयोग करेंगे।

BOOL hideKeyboardOnScroll;

2) संभाल UITextField प्रतिनिधि कॉलबैक:

#pragma mark - UITextFieldDelegate

- (void) textFieldDidBeginEditing: (UITextField *) textField {
    self.currentTextField = textField;
}

- (void) textFieldDidEndEditing: (UITextField *) textField {
    self.currentTextField = nil;
}

- (BOOL) textFieldShouldReturn: (UITextField *) textField {
   [textField resignFirstResponder];

    CGPoint newContentOffset = CGPointZero;
    if (tableView.contentSize.height > tableView.frame.size.height) {
        newContentOffset.y = MIN(tableView.contentOffset.y, tableView.contentSize.height - tableView.frame.size.height);
    }
    [tableView setContentOffset: newContentOffset animated: YES];

    return YES;
}

3) है कि उपयोगकर्ता स्क्रॉल दृश्य की जाँच करने के UIScrollViewDelegate विधि संभाल।

#pragma mark - UIScrollViewDelegate

- (void) scrollViewDidScroll: (UIScrollView *) scrollView {
    if (hideKeyboardOnScroll == YES) {
        [self.currentTextField resignFirstResponder];
    }
}

4) ViewController के [viewWillAppear] विधि में कुंजीपटल सूचनाओं की सदस्यता और [viewWillDisappear] विधि में सदस्यता समाप्त।

- (void) viewWillAppear: (BOOL) animated {
    [super viewWillAppear: animated];

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

- (void) viewWillDisappear: (BOOL) animated {
    [super viewWillDisappear: animated];

    [ [NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardDidShowNotification object: nil];
    [ [NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillHideNotification object: nil];    
}

5) कीबोर्ड सूचनाएं संभाल:

- (void) keyboardWillShow: (NSNotification *) notification {
    CGRect keyboardFrame = [ [ [notification userInfo] objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    // Find cell with textfield.
    CGRect textFieldFrame = [tableView convertRect: self.currentTextField.frame fromView: self.currentTextField];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: textFieldFrame.origin];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];
    //

    // Shrink tableView size.
    CGRect tableViewFrame = tableView.frame;
    tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width,
                             self.view.frame.size.height - tableView.frame.origin.y - keyboardFrame.size.height);
    //

    // Check if cell is visible in shrinked table size.
    BOOL cellIsFullyVisible = YES;
    if ( cell.frame.origin.y < tableView.contentOffset.y ||
        (cell.frame.origin.y + cell.frame.size.height) > (tableView.contentOffset.y + tableView.frame.size.height) ) {
        cellIsFullyVisible = NO;
    }
    //

    // If cell is not fully visible when scroll table to show cell;
    if (cellIsFullyVisible == NO) {
        CGPoint contentOffset = CGPointMake(tableView.contentOffset.x, CGRectGetMaxY(cell.frame) - tableView.frame.size.height);
        if (cell.frame.origin.y < tableView.contentOffset.y) {
            contentOffset.y = cell.frame.origin.y;
        }
        contentOffset.y = MAX(0, contentOffset.y);

        // For some reason [setContentOffset] is called without delay then
        // this code may not work for some cells. That why we call it with brief delay.
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [UIView animateWithDuration: 0.5 animations:^{
                [tableView setContentOffset: contentOffset animated: NO];
            } completion: ^(BOOL finished) {
                hideKeyboardOnScroll = YES;
            }];
        });
    } else {
        hideKeyboardOnScroll = YES;
    }
    //

    // Finally restore original table frame.
    tableView.frame = tableViewFrame;
    //
}

- (void) keyboardWillHide: (NSNotification *) notification {
    [super keyboardWillHide: notification];

    hideKeyboardOnScroll = NO;
}
21/08/2014 को 15:43
का स्रोत उपयोगकर्ता

वोट
0

मुझे लगता है कि सबसे अच्छा तरीका है UITableViewController माध्यम से है।

यदि आप एक UIViewController में एक UITableView चाहते हैं , बस एक एम्बेडेड UITableViewController के साथ एक contentView बनाने के लिए और UIViewController की viedDidLoad में निम्नलिखित लाइनों डाल:

self.tableView = ((UITableViewController*)self.childViewControllers[0]).tableView;
self.tableView.delegate = self;
self.tableView.dataSource = self;

आसान ;)

06/06/2014 को 16:29
का स्रोत उपयोगकर्ता

वोट
0

मुझे लगता है कि वहाँ कोई "सही" तरीका यह है है। आप अपने उपयोग के मामले के लिए सबसे उपयुक्त समाधान चुनना है। मेरी iPad एप्लिकेशन में मैं एक है UIViewControllerकि के रूप में मॉडल प्रस्तुत किया है UIModalPresentationFormSheetऔर एक के होते हैं UITableView। इस तालिका में दो शामिल UITextFieldsसेल प्रति। बस बुला scrollToRowAtIndexPath:atScrollPosition:animated:में textFieldDidBeginEditing:विधि मेरे लिए काम नहीं करता। इसलिए मैं एक बनाया है tableFooterView:

- (void)viewDidLoad
{
    [super viewDidLoad];

    m_footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, m_tableView.frame.size.width, 300.0f)];
    [m_footerView setBackgroundColor:[UIColor clearColor]];
    [m_tableView setTableFooterView:m_footerView];
    [m_footerView release];
}

विचार यह है कि कुंजीपटल छुपाता है tableFooterViewऔर नहीं UITextFields। तो tableFooterViewकाफी अधिक होना चाहिए। उसके बाद आप उपयोग कर सकते हैं scrollToRowAtIndexPath:atScrollPosition:animated:में textFieldDidBeginEditing:विधि।

मुझे लगता है कि यह भी पता चलता है और छिपाने के लिए संभव है tableFooterViewकुंजीपटल सूचनाओं के लिए पर्यवेक्षकों को जोड़कर गतिशील लेकिन मैं इसे अभी तक प्रयास नहीं किया है:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

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

- (void)keyboardWillShow:(NSNotification *)notification 
{
     [m_tableView setTableFooterView:m_footerView];
}

- (void)keyboardWillHide:(NSNotification *)notification 
{
     [m_tableView setTableFooterView:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
15/09/2012 को 08:51
का स्रोत उपयोगकर्ता

वोट
0

मैं एक छोटे से परियोजना है कि कुंजीपटल के साथ इस मुद्दे को हल करती है, मेरे मामले में मैं केवल जब कीबोर्ड दिखाता है तालिका दृश्य ऊपर जाना बनाने की जरूरत है बनाने के लिए किया था।

उम्मीद है की यह मदद करेगा!

http://git.io/BrH9eQ

19/11/2011 को 21:21
का स्रोत उपयोगकर्ता

वोट
0

मैं सिर्फ iOS 5.0 lib संदर्भ में फिर से देखा और पाया इस खंड शीर्षक "सामग्री कीबोर्ड के नीचे स्थित है यही कारण है कि बढ़ते": TextAndWebiPhoneOS KeyboardManagement

इस नए iOS 5 के बाद से शायद है? मैं इस पर अभी तक पढ़ा है नहीं के रूप में मैं कुछ और के बीच में हूँ, लेकिन शायद दूसरों अधिक जानते हैं और मुझे और दूसरों को यहाँ प्रबुद्ध कर सकते हैं।

एप्पल डॉक को अधिक्रमित करता है यहाँ क्या चर्चा की गई है या जानकारी यहां अभी भी iOS 5 एसडीके उपयोगकर्ताओं के लिए उपयोगी है?

26/10/2011 को 12:07
का स्रोत उपयोगकर्ता

वोट
0

UITableViewControllerस्क्रॉल, स्वचालित रूप से करता है वास्तव में। अंतर उपयोग की तुलना में एक UIViewControllerआप का उपयोग करके प्रोग्राम के रूप में नेवबार-Buttonitems बनाने के लिए जो है, NavigationControllerजब एक का उपयोग कर TableViewController

20/03/2011 को 20:59
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more