显示键盘时缩小所有视图

发布于 2024-10-12 03:56:48 字数 167 浏览 2 评论 0原文

我正在开发一个带有 UISplitViewController 的应用程序,并且想知道实现以下目标的最佳方法是什么:

在详细视图中,我有一个用于编辑 txt 文件的 UITextView。但是,当键盘出现在屏幕上时,我希望所有视图都缩小(主视图和细节视图)以反映新的尺寸。这可能吗?

谢谢

I am working on an app with a UISplitViewController and am wondering what the best way is to achieve the following:

In the detail view I have a UITextView for editing txt files. However, one the keyboard appears on screen, I want all of the views to shrink (master and detail) to reflect the new size. Is this possible?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-10-19 03:56:48

是的,这是可能的。您需要注册以下通知:

UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification。

并为每个指定处理程序。

然后在处理程序中,您需要调整/缩小 UITextView 的框架。请注意,通知将包含一个具有键盘尺寸的对象。您需要使用它们将文本视图缩小相应的量。

Here is some similar sample code you can refactor

 // Call this method somewhere in your view controller setup code.
 - (void)registerForKeyboardNotifications

{


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

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

keyboardShown=NO;

}
- (void)resizeTextView
{
    float margin=[contentTextBox frame].origin.x;
    //margin=0.0;
    CGRect rect0 = [[container scrollView] frame];
    CGRect rect1 = [contentTextBox frame];
    //shrink textview if its too big to fit with textview
    if(rect0.size.height<rect1.size.height){
        rect1.size.height=rect0.size.height-2*margin;
        [contentTextBox setFrame:rect1];        
    }
    //make the textview visible if content is bigger than scroll view
    if([[container scrollView] contentSize].height>[[container scrollView] frame].size.height){
        CGPoint point = CGPointMake([[container scrollView] contentOffset].x,[contentTextBox frame].origin.y-margin);
        [[container scrollView] setContentOffset:point animated:YES];       
    }
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{   
    //keyboard is already visible just exit
    if (keyboardShown)
        return;
    keyboardShown=YES;
    //resize the content text box and scroll into view
    if(activeTextView!=nil){
        [self resizeTextView];
    }
    //scroll text field into view
    if(activeTextField!=nil){
        //get the text field rectangle
        CGRect rect = [activeTextField frame];
        //adjust to the current page of the scroll view
        rect.origin.x=[[container scrollView] contentOffset].x;
        [[container scrollView] scrollRectToVisible:rect animated:YES];
    }

}
- (void)keyboardWasHidden:(NSNotification*)aNotification
{   
    //keyboard not visible just exit
    if (!keyboardShown)
        return;
    keyboardShown=NO;

    //resize the content text box
    if(contentTextBox!=nil){
        //find where the bottom of the texview should be from the label location
        CGRect rect0=[contentTextBoxBottom frame];
        //get the current frame
        CGRect rect1=[contentTextBox frame];
        //adjust width
        rect1.size.width=rect0.size.width;
        //adjust height
        rect1.size.height=rect0.origin.y-rect1.origin.y;
        //restore the size of the textview
        [contentTextBox setFrame:rect1];
    }
}

Yes its possible. You need to register for the following notifications,

UIKeyboardWillShowNotification and UIKeyboardWillHideNotification.

And designate handlers for each.

Then in the handlers you need to adjust/shrink the frame of the UITextView. Note the notification will include an object with the dimensions of the keyboard. You need to use these to shrink your textview by the corresponding amount.

Here is some similar sample code you can refactor

 // Call this method somewhere in your view controller setup code.
 - (void)registerForKeyboardNotifications

{


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

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

keyboardShown=NO;

}
- (void)resizeTextView
{
    float margin=[contentTextBox frame].origin.x;
    //margin=0.0;
    CGRect rect0 = [[container scrollView] frame];
    CGRect rect1 = [contentTextBox frame];
    //shrink textview if its too big to fit with textview
    if(rect0.size.height<rect1.size.height){
        rect1.size.height=rect0.size.height-2*margin;
        [contentTextBox setFrame:rect1];        
    }
    //make the textview visible if content is bigger than scroll view
    if([[container scrollView] contentSize].height>[[container scrollView] frame].size.height){
        CGPoint point = CGPointMake([[container scrollView] contentOffset].x,[contentTextBox frame].origin.y-margin);
        [[container scrollView] setContentOffset:point animated:YES];       
    }
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{   
    //keyboard is already visible just exit
    if (keyboardShown)
        return;
    keyboardShown=YES;
    //resize the content text box and scroll into view
    if(activeTextView!=nil){
        [self resizeTextView];
    }
    //scroll text field into view
    if(activeTextField!=nil){
        //get the text field rectangle
        CGRect rect = [activeTextField frame];
        //adjust to the current page of the scroll view
        rect.origin.x=[[container scrollView] contentOffset].x;
        [[container scrollView] scrollRectToVisible:rect animated:YES];
    }

}
- (void)keyboardWasHidden:(NSNotification*)aNotification
{   
    //keyboard not visible just exit
    if (!keyboardShown)
        return;
    keyboardShown=NO;

    //resize the content text box
    if(contentTextBox!=nil){
        //find where the bottom of the texview should be from the label location
        CGRect rect0=[contentTextBoxBottom frame];
        //get the current frame
        CGRect rect1=[contentTextBox frame];
        //adjust width
        rect1.size.width=rect0.size.width;
        //adjust height
        rect1.size.height=rect0.origin.y-rect1.origin.y;
        //restore the size of the textview
        [contentTextBox setFrame:rect1];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文