Storyboard 에서 iPad용 UI를 만들고 있는데, 가로/세로에 대해 서로 다른 UI를 만들어야 하는 경우가 생겼다.

문제는 iPad에서는 iPhone처럼 Regular/Compact을 조절해서는 안된다는 것.

아래와 같이 해결했음. 


UITraitCollection을 이용해서 화면에 따라 모드를 지정해 준다.


Storyboard 상에서는 아래 모드로 작업을 해두고

세로를 작업할 때는 Width:Any, Height:Regular 로 하고,

가로를 작업할 때는 Width:Regular, Height:Compact로 했음.


RootViewController나 ParentViewController에 아래 코드를 추가하면 된다는데, 나는 NavigationController에 해 줬음.


- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController

{

    UITraitCollection *wRegular = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];

    UITraitCollection *hCompact = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact];

    UITraitCollection *horizontal = [UITraitCollection traitCollectionWithTraitsFromCollections:@[wRegular, hCompact]];

    

    UITraitCollection *wAny = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassUnspecified];

    UITraitCollection *hRegular = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];

    UITraitCollection *portrait = [UITraitCollection traitCollectionWithTraitsFromCollections:@[wAny, hRegular]];

    

    if (CGRectGetWidth(self.view.bounds) < CGRectGetHeight(self.view.bounds))

    {

        return portrait;

    }

    else

    {

        return horizontal;

    }

}


참조 URL : http://stackoverflow.com/a/28268200

Posted by 프리지크
:

* Audio route 변경 시 Notification 수신 방법. (예. Headphones plugged/unplugged) 

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChanged:) name:AVAudioSessionRouteChangeNotification object:nil];


- (void)routeChanged:(NSNotification *)notification

{

   NSNumber *reason = [notification.userInfo objectForKey:AVAudioSessionRouteChangeReasonKey];

   

   if ([reason unsignedIntegerValue] == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {

   } else if ([reason unsignedIntegerValue] == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {       

   }

}


   - 연관 Notification : AVAudioSessionSilenceSecondaryAudioHintNotification, AVAudioSessionMediaServicesWereLostNotification, AVAudioSessionMediaServicesWereResetNotification, AVAudioSessionInterruptionNotification

Posted by 프리지크
:

* Headphone 이용 시 이벤트 수신 방법.

   // Headphones의 played/paused event 수신. (예. Apple EarPods 일 경우, Center Button 한번 클릭)

   // !!특이점

   //   - Apple EarPods 일 경우, togglePlayPauseCommand를 수신.

   //   - 일반 Bluetooth Headphones 일 경우, 아래의 playCommand/pauseCommand를 수신. (왜??? ;;;;)

   MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

   [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(command)];

   

   // Headphones의 played event 수신

   commandCenter.playCommand.enabled = YES;

   [commandCenter.playCommand addTarget:self action:@selector(playCommand)];

   

   // Headphones의 paused event 수신

   commandCenter.pauseCommand.enabled = YES;

   [commandCenter.pauseCommand addTarget:self action:@selector(pauseCommand)];

   

   // Headphones의 Next track event 수신. (예. Apple EarPods 일 경우, Volume up 버튼을 두번 클릭)

   commandCenter.nextTrackCommand.enabled = YES;

   [commandCenter.nextTrackCommand addTarget:self action:@selector(nextTrackCommand)];

   

   // Headphones의 Previous track event 수신. (예. Apple EarPods 일 경우, Volume down 버튼을 두번 클릭)

   commandCenter.previousTrackCommand.enabled = YES;

   [commandCenter.previousTrackCommand addTarget:self action:@selector(previousTrackCommand)];


또는, 


UIResponse를 상속받는 AppDelegate 에서 아래 메소드를 통해 이벤트를 받을 수 있음.


- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event


Posted by 프리지크
:

  예를 들어, 문자열을 4자리씩 끊어야 할 경우,


    NSString *number = @"1234567890";

    NSError *error = NULL;

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@".{1,4}" options:0 error:&error];

    NSArray *matches = [regex matchesInString:number options:0 range:NSMakeRange(0, [number length])];


    for (NSTextCheckingResult *match in matches)

    {

        NSLog(@"%@", [number substringWithRange:match.range]];

    }


결과는

 1234

 5678

 90


Posted by 프리지크
:

BLOG main image
인생에서는 찾고, 노력하고, 희생할 각오가 되어 있는 것만 얻을 수 있다. (조시 매슈 애덤스) by 프리지크

공지사항

카테고리

분류 전체보기 (121)
끄적끄적 (16)
좋은 글 (9)
자료 (19)
런닝&피트니스 (18)
기술 (43)
기사 스크랩 (1)
내가 본 공연 후기 (1)
내가 가 본 맛집 (5)
괜찮아 보이는 펜션들 (4)
(4)
한장한장 (0)
비공개 스크랩 (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :