2016-10-06 4 views
0

私はプログラムで作成されたUIViewサブクラスを持っています。 Autolayoutを使用する私のアプリケーションでは、ポートレートとランドスケープモードがあります。最初にUIViewフレームはinitWithFrameを使用して設定されます。しかし、向きが横向きに変わると、UIViewのCGRectがポートレートモードになってしまいます。UIViewサブクラスにデバイス向きのCGRectを変更するよう警告できますか?プログラムで作成されたデバイス方向のUIViewサブクラスのフレームを変更するにはどうすればよいですか?

誰でも私にこれを助けることができますか?

これはUIViewのための私の最初のフレームのセットアップコードです:

- (id)initWithFrame:(CGRect)frame videoUrl:(NSURL *)videoUrl{ 

self = [super initWithFrame:frame]; 

if (self) { 
    _frame_width = frame.size.width; 

    int thumbWidth = ceil(frame.size.width*0.05); 

    _bgView = [[UIControl alloc] initWithFrame:CGRectMake(thumbWidth-BG_VIEW_BORDERS_SIZE, 0, frame.size.width-(thumbWidth*2)+BG_VIEW_BORDERS_SIZE*2, frame.size.height)]; 
    _bgView.layer.borderColor = [UIColor grayColor].CGColor; 
    _bgView.layer.borderWidth = BG_VIEW_BORDERS_SIZE; 
    [self addSubview:_bgView]; 
+0

アプリの向きが変わったときの表示のサイズ、右? – vaibhav

+0

@vaibhavはい。UIView CGRectを変更する必要があります。 –

+0

私のansが合うかどうかチェックしてください。 – vaibhav

答えて

0

ちょうどそれがlandscapeまたはportraitモードであるかどうか、デバイスの向きを検出し、コードの下に表示さに応じてUIViewのサイズを変更:

デバイスの向きを検出する

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight) 
    { 
     UIView *view = [classObject setupView:xPos yPos:yPos width:widthValue height:heightValue]; 
     yourView.frame = view.frame; 
     NSLog(@"device orientation Landscapse"); 
    } 
    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    { 
     UIView *view = [classObject setupView:xPos yPos:yPos width:widthValue height:heightValue]; 
     yourView.frame = view.frame; 
     NSLog(@"device orientation portrait"); 
    } 
} 

クラス内にこのメソッドを書きます。どこからでも呼び出す必要はなく、ユーザーが回転するときに方向を検出します。

コード下記参照、ユーザが向きを変えるとき、完全なフレームと呼び、Xを送信することにより、y座標と幅と高さでUIViewを返しているクラス内のカスタムメソッドを作成します:あなたは、フレームを変更する必要が

-(UIView *) setupView:(float)x yPos:(float)y width:(float)width height:(float) height{ 

    UIView *yourView = [[UIView alloc] initWithFrame:CGRectMake(x,y,width,height)]; 
    yourView.backgroundColor=[UIColor clearColor]; 
    return yourView; 
} 
+0

私はこのビューUIViewサブビューをサブビューとしてビューコントローラに追加しています。方向変更を検出しています。問題は、UIViewサブクラスにトリガを送信して、最初はUIViewフレームはポートレートのみに設定されていますが、横長になるとフレームは変化しません。デバイスの向きに関するUIViewのデリゲートメソッドにトリガーを送信するための方法が必要です。 –

+0

クラス内にビューを設定して作成するために作成したメソッドはありますか?そのクラスコードを表示してください。 – vaibhav

+0

"[self.view addSubview:_videoRangeSlider];"私はこのように追加しました。私は、CGViewを変更しなければならない別個のUIViewサブクラスと、UIViewがサブビューとして追加された別のViewControllerを持っています。ViewControllerからUIViewサブクラスにオリエンテーションをトリガする必要があります。 –

関連する問題