【问题标题】:iphone: scroll in a viewiphone:在视图中滚动
【发布时间】:2011-07-16 10:45:05
【问题描述】:

我想问一个基本的 iphone 问题。我在 iPhone 视图中有很多 TextField,当我点击输入 TextField 时,键盘会显示并隐藏其他 TextField。我想让父视图可滚动。请给我看示例代码好吗?

非常感谢

【问题讨论】:

    标签: iphone view uiscrollview uiscrollviewdelegate


    【解决方案1】:

    您可以收听键盘向上和向下通知。并将您的视图移动到键盘高度上方。

    ViewWillAppear 方法中:

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

    ViewWillDisAppear 方法中:

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

    然后有上面提到的方法来调整bar的位置:

    -(void) keyboardWillShow:(NSNotification *) note
    {
        CGRect r  = bar.frame, t;
        [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
        r.origin.y -=  t.size.height;
        bar.frame = r;
    }
    
    
    
    
     -(void) keyboardWillHide:(NSNotification *) note
        {
            CGRect r  = bar.frame, t;
            [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
            r.origin.y +=  t.size.height;
            bar.frame = r;
        }
    

    【讨论】:

    • 谢谢,但在这种情况下视图不能滚动。如果我想向上/向下滚动以查看其他文本字段,我不能这样做。您还有其他解决方案吗?
    • 尝试将所有视图放入 UIScrollView 中(这将被称为容器视图),然后在 self.view addSubViews:iMyUIScrollView 中添加 tis UIScrollView ...如果有任何困难,请告诉我...跨度>
    • 我将所有的 TextField 添加到一个 UIScrollView 中,该 ScrollView 是 UIView (MyViewController) 的子视图。但是我仍然无法滚动其中包含许多 TextField 的视图。我想我是在用 UIScrollView 操作,对吗?
    • 现在,您现在可以在键盘顶部看到您的视图了吗?
    • 使用苹果示例代码来处理滚动视图的developer.apple.com/library/ios/#samplecode/Scrolling/…
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      如果父视图是 UIScrollView 则尝试在文本字段委托中类似

      - (BOOL) textFieldShouldReturn:(UITextField *)theTextField { if (theTextField == textFieldName) { [scroll scrollRectToVisible:CGRectMake(0, 160, 280, 440) animated:YES];//相应地选择矩形。 } 返回是; }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多