如何调整ios视图中的视图对象?

时间:2013-03-17 02:12:04

标签: ios

我是ios app开发的新手。 在示例登录页面中,视图中有许多视图对象。当我尝试编辑屏幕底部的文本字段时,键盘出现并且它位于我需要编辑的视图对象上。因此,视图对象用键盘本身覆盖。 即使出现键盘,如何正确查看屏幕底部的所有视图对象。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

添加滚动视图,在其上显示控件,添加键盘通知,调整滚动内容。

有一份好的文件here

 - (void)viewDidLoad
{
    @try
    {
        [super viewDidLoad];


        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];

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

    }
    @catch (NSException *exception)
    {
        NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
    }

}
- (void)keyboardWasShown:(NSNotification *)notification
{

    @try
    {
        // Step 1: Get the size of the keyboard.
        CGSize keyboardSizePotriat = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

        CGSize keyboardSize = {keyboardSizePotriat.height,keyboardSizePotriat.width};

        // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
        self.theScrollView.contentInset = contentInsets;
        self.theScrollView.scrollIndicatorInsets = contentInsets;

        CGPoint scrollPoint = CGPointMake(0.0, self.txtMRN.frame.origin.y - (keyboardSize.height - 45));

        [self.theScrollView setContentOffset:scrollPoint animated:YES];

    }
    @catch (NSException *exception)
    {
        NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
    }

}

- (void) keyboardWillHide:(NSNotification *)notification
{
    @try
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.theScrollView.contentInset = contentInsets;
        self.theScrollView.scrollIndicatorInsets = contentInsets;

    }
    @catch (NSException *exception)
    {
        NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
    } 
}