IT박스

iOS 5와 하위 호환성을 유지하면서 iOS 6에서 자동 레이아웃 활성화

itboxs 2020. 6. 8. 21:14
반응형

iOS 5와 하위 호환성을 유지하면서 iOS 6에서 자동 레이아웃 활성화


이전 버전의 iOS에서 이전 장치와의 호환성을 제공하면서 iOS 6의 새로운 자동 레이아웃 기능을 활용하는 가장 좋은 방법은 무엇입니까?


각 .storyboard 또는 .xib 파일에서 자동 레이아웃을 활성화하거나 비활성화 할 수 있습니다. 특정 파일을 선택하고 Xcode의 파일 관리자를 사용하여 "자동 레이아웃 사용"속성을 수정하십시오.

파일 관리자의 자동 레이아웃 속성

6.0 이전의 배치 대상이 iOS 버전으로 설정된 자동 레이아웃 가능 인터페이스 파일을 사용하면 컴파일 오류가 발생합니다. 예를 들면 다음과 같습니다.

MainStoryboard.storyboard : 3의 오류 : 6.0 이전의 iOS 버전에서 자동 레이아웃

프로젝트에서 자동 레이아웃을 사용하고 여전히 iOS4-5와의 호환성을 유지하는 옵션 중 하나는 두 개의 대상 을 작성 하는 것입니다 . 하나는 배치 대상 iOS 6.0 및 다른 하나는 이전 iOS 버전입니다.

여기에 이미지 설명을 입력하십시오

스토리 보드 및 XIB 파일 각각에 대해 두 가지 버전을 만들 수 있으며 6.0 대상에서 활성화 된 자동 레이아웃과 레거시 대상에서 다른 레이아웃을 사용할 수 있습니다.

여기에 이미지 설명을 입력하십시오

그런 다음 MainStoryBoardAutoSize를 iOS6 대상의 빌드 단계에 추가하고 다른 파일을 iOS4 대상에 추가하십시오. 여러 대상 사용에 대한 자세한 내용은 여기를 참조하십시오 .

편집 : marchinram의 답변에서 지적했듯이 코드에서 스토리 보드 파일을로드하고 Xcode의 "주 스토리 보드"설정을 사용하여 초기 스토리 보드를 설정하지 않으면 단일 대상을 사용할 수 있습니다.

나를 위해 여러 대상과 인터페이스 파일을 유지 관리하는 데 따른 복잡성의 추가 비용이 자동 레이아웃 사용의 이점보다 큰 것 같습니다. 몇 가지 특별한 경우를 제외하고 iOS4-5 호환성이 필요한 경우 일반 오래된 자동 크기 조정 (또는 코드의 layoutSubViews)을 사용하는 것이 훨씬 좋습니다.


정말로 두 개의 목표가 필요합니까? 나는 이와 같이 작동하고 Imre Kelényi와 같은 2 개의 스토리 보드가 있는데 하나는 자동 레이아웃을 사용하고 다른 하나는 사용하지 않고 다른 하나는 응용 프로그램 대리자에서 사용중인 버전을 확인하고 올바른 스토리 보드를 선택합니다.

#import "AppDelegate.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:(v) options:NSNumericSearch] != NSOrderedAscending)

@interface AppDelegate ()
    @property (strong, nonatomic) UIViewController *initialViewController;
@end

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *mainStoryboard = nil;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
        mainStoryboard = [UIStoryboard storyboardWithName:@"iPhone_iOS6" bundle:nil];
    } else {
        mainStoryboard = [UIStoryboard storyboardWithName:@"iPhone_iOS5" bundle:nil];
    }

    self.initialViewController = [mainStoryboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.initialViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

@end

2 개의 목표를 가진 것은 잘 작동하지만 나에게 과잉 인 것 같습니다


레이아웃 차이가 크지 않으면 스프링과 스트럿사용 하여 요소를 배치 하는 것이 훨씬 쉽습니다 .


Inspired by @marchinram's one target idea, this is the solution I finally came up with. Two storyboards, one for struts-and-springs and one for autolayout. In the target summary, I set the autolayout storyboard as the default. Then, in the appDelegate, I check whether I need to load the pre-6.0 struts-and-springs storyboard after all:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    Class cls = NSClassFromString (@"NSLayoutConstraint");
    if (cls == nil) {
        NSString *mainStoryboardName = nil;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            mainStoryboardName = @"MainStoryboard_iPad_StrutsAndSprings";
        } else {
            mainStoryboardName = @"MainStoryboard_iPhone_StrutsAndSprings";
        }
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:mainStoryboardName bundle:nil];

        UIViewController *initialViewController = [mainStoryboard instantiateInitialViewController];
        self.window.rootViewController = initialViewController;
        [self.window makeKeyAndVisible];
    }

Also, I set the deployment target of the struts-and-springs storyboard to iOS 5.1, and that of the autolayout storyboard to Project SDK(iOS 6.0).

I really wanted to do the switch before the default in storyboard is loaded, in willFinishLaunchingWithOptions: but that results in an 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named NSLayoutConstraint' no matter what I tried.


RRAutoLayout을 사용하십시오 : https://github.com/RolandasRazma/RRAutoLayout iOS5에 대한 iOS6 AutoLayout 백 포트입니다.


Xibs 기본보기 크기를 Freeform으로 설정 한 다음 자동 크기 조정을 사용하면 문제가 없습니다. 뷰 문제에 대한 코드가 엉망이 아닙니다.

참고 URL : https://stackoverflow.com/questions/12411980/enabling-auto-layout-in-ios-6-while-remaining-backwards-compatible-with-ios-5

반응형