0

テキストフィールドがどの向きでも上に移動する必要があります。

しかし、横向きと縦向きが逆さまになっていると問題が発生します。

それらのいずれかに向けると、テキストフィールドを下に向けると、ビューは上がらず、上にあるテキストフィールドが上になります。

いくつかの解決策を提案してください。

以下は、オリエンテーションに使用しているコードです。

私は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];
}
4

1 に答える 1

0

ViewController で - willRotateToInterfaceRotation:duration メソッドをオーバーライドします。このメッセージは、ユーザーがデバイスを回転させたときに呼び出されます。この方法では、すべてのビューを適切な位置に配置するように設定できます。リファレンスのビュー回転イベントへの応答を参照してください: UIViewController リファレンス

次のようなことができます。

    - (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
}

次に、回転時に正しく配置されていないすべてのビューに対してそれを行います

于 2011-08-11T12:17:54.413 に答える