6

注:これは、ルートビューコントローラーに追加されたときにサブビューが自動サイズ設定されないのと重複している可能性があります


メインウィンドウでさまざまなビューを切り替えるiPadアプリがあります。ビュー切り替えコードは次のようになります。

- (void)switchToViewController:(UIViewController*)viewController {
    if (currentViewController != viewController) {
        [currentViewController.view removeFromSuperview];
        currentViewController = viewController;
        [window addSubview:viewController.view];
    }
}

問題は、新しいビュー(UISplitView)が横向きで表示される場合、ウィンドウ全体を埋めるサイズになっていないことです。右側に大きな空の黒いスペースがあります。ビューの幅は、横向きのウィンドウの1024ピクセルの幅ではなく、わずか768ピクセルのようです。

デバイスを縦向きに回転させてから横向きに戻すと、ビュー自体のサイズが適切になります。

デバイスが縦向きの場合、すべてが正常に機能します。UISplitViewが最初に表示するビューの場合も、適切なサイズになります。この問題は、横向きで別のビューが表示された後で切り替える場合にのみ発生します。

それで、ウィンドウに追加された後にiPhone OSにビューのサイズを変更させる方法はありますか?

sizeToFit、、を呼び出してみましたsetNeedsLayout。また、ビューboundsをウィンドウに設定してみました。また、前のビューのフレームに一致するようにbounds設定してみました。frame

4

6 に答える 6

9

これは絶対に可能です!:-)

ここで私のリポジトリをチェックできます: https ://github.com/hfossli/AGWindowView

回転やフレームの変更を自動的に処理するので、心配する必要はありません。

それを心配したい場合は、最も重要な部分をカットアンドペーストするだけです

#1ウィンドウにビューを追加

[[UIApplication sharedApplication] keyWindow] addSubview:aView];

#2リスナーを追加してビューを更新

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameOrOrientationChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

通知リスニングを削除することを忘れないでください

[[NSNotificationCenter defaultCenter] removeObserver:self];

#3数学を行う

- (void)statusBarFrameOrOrientationChanged:(NSNotification *)notification
{
    /*
     This notification is most likely triggered inside an animation block,
     therefore no animation is needed to perform this nice transition.
     */
    [self rotateAccordingToStatusBarOrientationAndSupportedOrientations];
}

- (void)rotateAccordingToStatusBarOrientationAndSupportedOrientations
{
    UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
    CGFloat angle = UIInterfaceOrientationAngleOfOrientation(statusBarOrientation);
    CGFloat statusBarHeight = [[self class] getStatusBarHeight];

    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
    CGRect frame = [[self class] rectInWindowBounds:self.window.bounds statusBarOrientation:statusBarOrientation statusBarHeight:statusBarHeight];

    [self setIfNotEqualTransform:transform frame:frame];
}

- (void)setIfNotEqualTransform:(CGAffineTransform)transform frame:(CGRect)frame
{
    if(!CGAffineTransformEqualToTransform(self.transform, transform))
    {
        self.transform = transform;
    }
    if(!CGRectEqualToRect(self.frame, frame))
    {
        self.frame = frame;
    }
}

+ (CGFloat)getStatusBarHeight
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(UIInterfaceOrientationIsLandscape(orientation))
    {
        return [UIApplication sharedApplication].statusBarFrame.size.width;
    }
    else
    {
        return [UIApplication sharedApplication].statusBarFrame.size.height;
    }
}

+ (CGRect)rectInWindowBounds:(CGRect)windowBounds statusBarOrientation:(UIInterfaceOrientation)statusBarOrientation statusBarHeight:(CGFloat)statusBarHeight
{    
    CGRect frame = windowBounds;
    frame.origin.x += statusBarOrientation == UIInterfaceOrientationLandscapeLeft ? statusBarHeight : 0;
    frame.origin.y += statusBarOrientation == UIInterfaceOrientationPortrait ? statusBarHeight : 0;
    frame.size.width -= UIInterfaceOrientationIsLandscape(statusBarOrientation) ? statusBarHeight : 0;
    frame.size.height -= UIInterfaceOrientationIsPortrait(statusBarOrientation) ? statusBarHeight : 0;
    return frame;
}

CGFloat UIInterfaceOrientationAngleOfOrientation(UIInterfaceOrientation orientation)
{
    CGFloat angle;

    switch (orientation)
    {
        case UIInterfaceOrientationPortraitUpsideDown:
            angle = M_PI;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            angle = -M_PI_2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            angle = M_PI_2;
            break;
        default:
            angle = 0.0;
            break;
    }

    return angle;
}

UIInterfaceOrientationMask UIInterfaceOrientationMaskFromOrientation(UIInterfaceOrientation orientation)
{
    return 1 << orientation;
}

幸運を!

于 2011-02-10T18:14:33.290 に答える
3

これは機能しますが、少しハッキーなようです。

- (void)switchToViewController:(UIViewController *)viewController {
    if (viewController != currentViewController) {
        UIInterfaceOrientation orientation = currentViewController.interfaceOrientation;
        [currentViewController.view removeFromSuperview];

        currentViewController = viewController;
        UIView *view = viewController.view;

        // Set appropriate view frame (it won't be autosized by addSubview:)
        CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            // Need to flip the X-Y coordinates for landscape
            view.frame = CGRectMake(appFrame.origin.y, appFrame.origin.x, appFrame.size.height, appFrame.size.width);
        }
        else {
            view.frame = appFrame;
        }

        [window addSubview:view];
    }
}
于 2010-04-18T05:02:56.483 に答える
1

ウィンドウには、ビュー以外のUI要素が含まれる場合があります。この例の20ピクセルの違いは、ステータスバーの高さです。

[[UIApplication sharedApplication] statusBarFrame].height;

ウィンドウも画面も回転しません。フレームを取得して回転ビューに使用することは、高さと幅を切り替えた場合にのみ機能します。

UIViewControllerを使用している場合は、次のメソッドからYESを返してみてください。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; // Override to allow rotation. Default returns YES only for UIDeviceOrientationPortrait
于 2010-04-17T18:36:31.740 に答える
1

同じ問題が発生しましたが、次のコード行で修正しました。

- (void)changeRow:(NSNotification *)notification {
[window addSubview:new.view];
[old.view removeFromSuperview];
[new.view removeFromSuperview];
[window addSubview:new.view];

}

新しいビューを追加してから、古いビューと新しいビューを削除してから、新しいビューを追加する必要があります。理由はわかりませんが、うまくいきます。

于 2010-05-03T12:01:47.420 に答える
1

Fossliの答えはiPadにとって正しいです。ただし、サポートする必要のあるユニバーサルアプリがあります。したがって、いくつかの調整が必要です。

AppDelegate.hに以下を追加します

@property (strong, nonatomic) UIImageView *imageView;

AppDelegate.mに以下を追加します

@synthesize imageView;

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;   
    if (! (UIInterfaceOrientationIsLandscape(deviceOrientation) ||
           UIInterfaceOrientationIsPortrait(deviceOrientation)))
    {
        // May be "UIInterfaceOrientationUnknown" which does not appear to be a defined value anywhere.
        return;
    }

    [imageView setImage:[UIImage imageNamed:[Utility getBackgroundImageNameWithOrientation:deviceOrientation]]];

    /*
     iOS Image Sizes

     iPhone/iPod    Portrait 320 x 480 (640 x 960 @2x)
     iPad           Portrait 768 x 1004 (1536 x 2008 @2x)
                    Landscape 1024 x 748 (2048 x 1496 @2x)

     iPad window bounds in both orientations 768 x 1024  (needs manual swap in landscape)
     iPhone window bounds in both orientations 320 x 480 (needs manual swap in landscape)

     Note the size variations between the required default launch image sizes and
     the size of the window bounds.
     iPhone/iPod only requires rotations.
     iPad needs origin or size adjustments depending on orientation.
     */

    CGFloat angle = 0.0;
    CGRect newFrame = [[self window] bounds];

    // How to get size of status bar
    // Size of status bar gets all wonky on rotations so just set it manually
    // CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
    CGSize statusBarSize = CGSizeMake(20.0, 20.0);

    if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        angle = M_PI; 
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            newFrame.size.height -= statusBarSize.height;
        }
    }
    else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        angle = - M_PI / 2.0f;        
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            newFrame.origin.x += statusBarSize.height;
            newFrame.size.width += statusBarSize.height;
        }
    }
    else if (deviceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        angle = M_PI / 2.0f;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            newFrame.size.width -= statusBarSize.height;
        }
    }
    else
    {
        angle = 0.0;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            newFrame.origin.y += statusBarSize.height;
            newFrame.size.height -= statusBarSize.height;
        }
    } 

    imageView.transform = CGAffineTransformMakeRotation(angle);
    imageView.frame = newFrame;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    // Add background image to window with orientation changes so that it is visible in all views.
    // A listener is added since subviews do not receive orientation changes.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object: nil];

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;      
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[Utility getBackgroundImageNameWithOrientation:deviceOrientation]]];
    [[self window] addSubview:imageView];

    return YES;
}

Utility.hに以下を追加します

+ (NSString *)getBackgroundImageNameWithOrientation:(UIDeviceOrientation)interfaceOrientation;

Utility.mに以下を追加します

+ (NSString *)getBackgroundImageNameWithOrientation:(UIDeviceOrientation)interfaceOrientation
{
    NSString *imageName = nil;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
        {
            imageName = @"Default-Landscape~ipad.png";
        }
        else
        {
            imageName = @"Default-Portrait~ipad.png";
        }
    }
    else
    {
        if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
        {
            imageName = @"Default-Landscape~iphone.png";
        }
        else
        {
            imageName = @"Default.png";
        }
    }

    return imageName;
}
于 2012-06-12T20:31:34.187 に答える
0

iOS7のウィンドウは、iOS8/9のウィンドウとは異なる動作をします。

iOS7のキーボードウィンドウとiOS8/9のすべてのウィンドウは、常に正しい向きとサイズになっています。したがって、サイズ変更イベントを監視し、ビューのフレームを更新できます。

ただし、iOS7の他のウィンドウは、常に縦向きとサイズを維持します。回転後にビューの変換を更新する必要があります。

UIApplicationWillChangeStatusBarOrientationNotificationをオブザーバーし、UIViewのサイズを次のように更新する必要があります。

@interface MyView : UIView

@end

@implementation MyView

- (instancetype)init
{
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientationHandler:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
}

- (void)updateTransformWithOrientation:(UIInterfaceOrientation)orientation
{
    CGFloat width = CGRectGetWidth(self.window.bounds);
    CGFloat height = CGRectGetHeight(self.window.bounds);
    if (width > height) {
        CGFloat temp = width;
        width = height;
        height = temp;
    }
    CGFloat offset = (height - width) / 2;
    CGAffineTransform transform;
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            transform = CGAffineTransformMakeTranslation(-offset, offset);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIInterfaceOrientationLandscapeRight:
            transform = CGAffineTransformMakeTranslation(-offset, offset);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            transform = CGAffineTransformMakeRotation(-M_PI);
            break;
        default:
            transform = CGAffineTransformIdentity;
            break;
    }
    self.transform = transform;
    self.frame = CGRectMake(0, 0, width, height);
}

- (void)updateFrameWithOrientation:(UIInterfaceOrientation)orientation
{
    CGFloat width = CGRectGetWidth(self.window.bounds);
    CGFloat height = CGRectGetHeight(self.window.bounds);
    if (width > height) {
        CGFloat temp = width;
        width = height;
        height = temp;
    }
    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            self.frame = CGRectMake(0, 0, height, width);
            break;
        default:
            self.frame = CGRectMake(0, 0, width, height);
            break;
    }
}

- (void)updateWithOrientation:(UIInterfaceOrientation)orientation
{
    BOOL isIos7 = [[UIDevice currentDevice].systemVersion floatValue] < 8.0;
    BOOL isKeyboardWindow = [self.window isKindOfClass:NSClassFromString(@"UITextEffectsWindow")];
    if (isIos7 == YES && isKeyboardWindow == NO) {
        [self updateTransformWithOrientation:orientation];
    } else {
        [self updateFrameWithOrientation:orientation];
    }
}

- (void)changeOrientationHandler:(NSNotification *)notification
{
    [UIView animateWithDuration:0.25 animations:^{
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        [self updateWithOrientation:orientation];
    }];
}

@end
于 2016-02-25T03:42:01.673 に答える