'DrawRect'에 해당되는 글 1건

  1. 2012.09.03 iOS5 Customizing Navigation Bar (네비게이션바 커스터마이징)
iOS2012. 9. 3. 14:52

iOS5 업데이트 후 이전에 만들어 놓았던 어플중 Navigation Bar가 초기화 되어서 보여지는 현상을 발견했다.

기존에 대부분의 개발자분들이 사용하던 방법은 UINavigationBar 라는 Class의 Category 형태로 만들어
UIView의 drawRect: 를 override해서 적용하던 방식이였던걸로 알고 있다.

그러나 iOS5로 업데이트 후 위에 명시한 UINavigationBar Category에서 명시한 drawRect 메서드가 호출되지 않는다.
그래서 애플 공식 문서에서 UINavigationBar에 대해서 찾아보았더니 아래와 같은 내용이 보인다.

Prior to iOS v5.0, when used in conjunction with a navigation controller, there are only a handful of direct customizations you can make to the navigation bar. Specifically, it is alright to modify the barStyletintColor, and translucent properties, but you must never directly change UIView-level properties such as the frameboundsalpha, or hidden properties directly. In addition, you should let the navigation controller manage the stack of navigation items and not attempt to modify these items yourself.

In iOS v5.0 and later, you can customize the appearance of the bar using the methods listed in “Customizing the Bar Appearance.” You can customize the appearance of all navigation bars using the appearance proxy ([UINavigationBar appearance]


잘은 모르겠지만.. UINavigation Controller Class에서 UIView-level properties를 직접 변경하지 못한다(??) 라고 적혀 있는 것 같다. 그렇다면 당연히 drawRect로 호출하지 못하는게 당연하다는 결론이 나온다.

해결방법은..
따라서 애플 공식 문서에서 말한 방법으로 “Customizing the Bar Appearance.” 해결 할 수 있다.

(1) iOS5 이하 버젼에서는 기존과 같은 방식으로 Category를 사용하면 되고
(2)  iOS5 이상 버젼에서는 아래와 같은 방식으로 처리한다. (iOS5 이하에서는 실행되지 않는다.)

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {

        NSLog(@"iphone 5 이상");

        //iOS 5 new UINavigationBar custom background

        UIImage *image = [UIImage imageNamed:@"이미지명"];

        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

    }  



* setBackgroundImage:forBarMetrics:

Sets the background image for given bar metrics.

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics

Parameters

backgroundImage

The background image to use for barMetrics.

barMetrics

A bar metrics constant.

Availability

  • Available in iOS 5.0 and later.

Declared In

UINavigationBar.h

Posted by 다오나무