2011-08-11 70 views
0

我希望我的文本框在任何方向上向上移動。方向問題

但是我在橫向右轉和縱向倒置時出現問題。

因爲當我將其定位到其中的任何一個文本框時,我的文本框向下看不到,但頂部的文本框正在向上。

建議我一些解決方案。

下面是我用於定位的代碼。

請建議我整個過程,如果你可以,因爲我是一個niwBie到iPhone開發。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField]; 
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view]; 

CGFloat midLine=textFieldRect.origin.y+0.5*textFieldRect.size.height; 
CGFloat numerator=midLine-viewRect.origin.y-MINIMUM_SCROLL_FRACTION*viewRect.size.height; 
CGFloat denominator=(MAXIMUM_SCROLL_FRACTION-MINIMUM_SCROLL_FRACTION)*viewRect.size.height; 

CGFloat heightFraction=numerator/denominator; 
if(heightFraction < 0.0) 
{ 
    heightFraction=0.0; 
} 
else if(heightFraction > 1.0) 
{ 
    heightFraction=1.0; 
} 

CGRect viewFrame; 
UIInterfaceOrientation orientation=[[UIApplication sharedApplication]statusBarOrientation]; 
//code for text field editing in landscape an portrait mode 

if(orientation==UIInterfaceOrientationPortrait||orientation==UIInterfaceOrientationPortraitUpsideDown) 
{ 
    animatedDistance=floor(PORTRAIT_KEYBOARD_HEIGHT*heightFraction); 
} 
else 
{ 
    animatedDistance=floor(LANDSCAPE_KEYBOARD_HEIGHT*heightFraction); 
} 

if (orientation==UIInterfaceOrientationPortrait) { 
    viewFrame=self.view.frame; 
    viewFrame.origin.y-=animatedDistance; 
} 
else if(orientation==UIInterfaceOrientationPortraitUpsideDown){ 
    viewFrame=self.view.frame; 
    viewFrame.origin.y+=animatedDistance; 
} 
else if(orientation == UIInterfaceOrientationLandscapeRight){ 
    viewFrame=self.view.frame; 
    viewFrame.origin.x+=animatedDistance; 

} 
else if(orientation == UIInterfaceOrientationLandscapeLeft){ 

    viewFrame=self.view.frame; 
    viewFrame.origin.x-=animatedDistance; 
} 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
[self.view setFrame:viewFrame]; 

[UIView commitAnimations]; 
} 

回答

0

在你的ViewController中重寫 - willRotateToInterfaceRotation:duration方法。當用戶旋轉設備時會調用此消息。在這種方法中,您可以將所有視圖設置在適當的位置。請參閱響應視圖旋轉活動的參考:UIViewController reference

你可以做這樣的事情:旋轉

+0

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){ yourView.frame = CGRectMake(x,y,w,h); //and set the proper coordinate for this view in this orientation } 

,然後做你所有的觀點,即放置不正確,但我不知道如何在我的風景正確和顛倒模式下...視圖中的文本字段正在用鍵盤覆蓋...我將寫入什麼設置 - willRotateToInterfaceRotation:持續時間方法?...請通過查看我的代碼以上解釋我textFieldDidBeginEditing方法... O_o –

+0

請看上面。我添加了一些代碼。 – broch

+0

其實在我的iPhone應用程序中,我使用上面的代碼來提升視圖以顯示鍵盤上方的文本字段。我的應用程序支持所有四種接口方向。我使用下面的代碼來解除視圖。這裏的問題是,它只適用於縱向。有沒有什麼辦法可以在所有四個方向上實施正確的提升 –