2011-03-07 85 views
3

我知道這是一個經常遇到的問題,但是我沒有從我找到的例子中實現這個功能。我可以做一些簡單的錯誤..(讓希望如此)。我正在嘗試將UIview作爲隱藏在底部的文本字段提升。當鍵盤顯示時向上移動UIView

有一點關於我的應用程序,它有一個標籤欄控制器,並實現一個標準的UIView。當我添加代碼來移動顯示時沒有任何反應。我是否需要有一個滾動視圖才能正常工作,是否與Tab控制器發生衝突,或者我是否做了其他錯誤?感謝

+3

嘗試發佈你如何移動UIView的一些代碼 - 可以看到你去哪裏錯了 – Luke 2011-03-07 14:25:31

+0

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-當鍵盤提示是一個教程,但我現在已經跟幾個。應該是修改文本字段的情況。 – Dan 2011-03-07 14:41:30

+0

我總是收到'MainViewController'的警告可能不會響應'-setViewMovedUp:' – Dan 2011-03-07 14:42:17

回答

6

我寫了一篇關於UIView的一個小類別管理暫時圍繞滾動的東西,而無需包住整個事情到一個UIScrollView。我在這裏使用動詞「scroll」可能並不理想,因爲它可能會讓你覺得有一個滾動視圖,並沒有 - 我們只是動畫UIView(或UIView子類)的位置。

有一些嵌入在這個適合我的形式和佈局,可能不適合你的神奇數字,所以我鼓勵調整,以適應您的具體需求。

的UIView + FormScroll.h:

#import <Foundation/Foundation.h> 

@interface UIView (FormScroll) 

-(void)scrollToY:(float)y; 
-(void)scrollToView:(UIView *)view; 
-(void)scrollElement:(UIView *)view toPoint:(float)y; 

@end 

的UIView + FormScroll.m:

#import "UIView+FormScroll.h" 


@implementation UIView (FormScroll) 


-(void)scrollToY:(float)y 
{ 

    [UIView beginAnimations:@"registerScroll" context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:0.4]; 
    self.transform = CGAffineTransformMakeTranslation(0, y); 
    [UIView commitAnimations]; 

} 

-(void)scrollToView:(UIView *)view 
{ 
    CGRect theFrame = view.frame; 
    float y = theFrame.origin.y - 15; 
    y -= (y/1.7); 
    [self scrollToY:-y]; 
} 


-(void)scrollElement:(UIView *)view toPoint:(float)y 
{ 
    CGRect theFrame = view.frame; 
    float orig_y = theFrame.origin.y; 
    float diff = y - orig_y; 
    if (diff < 0) { 
     [self scrollToY:diff]; 
    } 
    else { 
     [self scrollToY:0]; 
    } 

} 

@end 

導入到你的UIViewController,然後你可以做

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self.view scrollToView:textField]; 
} 

-(void) textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self.view scrollToY:0]; 
    [textField resignFirstResponder]; 
} 

.. 。管他呢。該類別爲您提供了三種很好的方式來調整視圖的位置。

+0

我似乎得到UIView可能無法響應滾動查看。任何想法 – Dan 2011-03-07 21:10:25

+1

#import「UIView + FormScroll.h」 – 2011-03-08 14:17:57

1

最簡單的方法是將您的表單放置在UITableViewCell中,並將單元格放置在UITableView上。如果那麼,UITableView將自動處理大部分作品,並且與Apple應用程序完全相同。我在其他文章或答案中看到了這一點,但我不記得我在哪看到這個。

但是我試着實現模擬UITableView的行爲,這非常困難。大部分功能都很簡單,但在罕見情況下,我無法複製特殊行爲。所以我只是選擇使用UITableView

相關問題