2013-10-09 3 views
0

내 UINavigationBar의 사용자 정의 모양을 만들려고합니다. 사용자가 앱을 가로 방향으로 배치하면 탐색 표시 줄이 이미지로 덮여 야합니다. 다시 세로로 회전하면보기 (이미지)가 제거됩니다. 제거하는 것을 제외하고는 아래의 코드로 모든 것이 잘 작동합니다. 같은 if 문에 코드를 삽입하면 View를 제거 할 수 있지만 else if 문에 넣으면 아무 일도 발생하지 않습니다. 내가 놓친 게 있니?/감사합니다UINavigationBar에 subView 추가 및 제거

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation { 

    UIView *v = [[UIView alloc]init]; 

    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
{ 

    v.frame = CGRectMake(0,0,480,44); 
    v.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"navbar25g.png"]]; 
    [self.view addSubview:v]; 
    //[v removeFromSuperview]; <----- works if I put it here 

    NSLog(@"LANDSCAPE");//load the landscape view 

} 
    else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 
      [v removeFromSuperview]; <----- Not working 


     NSLog(@"PORTRAIT"); //load the portrait view 

    } 

} 

답변

1

UIView * v의 새 인스턴스를 만드는 방법을 사용할 때마다 제거 할 수없는 이유가 있습니다.

인스턴스 변수를 생성 한 다음 뷰에 할당하십시오. 일단 할당되면 필요에 따라 제거하거나 추가 할 수 있습니다.

당신은 이제 당신은 당신이 추가하고 그것을 제거 할 수있는 다음 뷰를 태그 즉

UIView *v = nil; 

v = [self.view viewForTag:1000]; 

if (!v) { 
    v = [[UIView alloc] init]; 
    v.tag = 1000; 
    v.frame = CGRectMake(0, 0, 480, 44); 
    v.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"navbar25g.png"]]; 
} 

을 줄 수있는 인스턴스 변수를 사용하지 않으려면.

0

UIAppearance를 사용해 보셨습니까?

[[UINavigationBar appearance] setBackgroundImage:nil barMetrics:UIBarMetricsDefault]; 
    [[UINavigationBar appearance] setBackgroundImage:image barMetrics:UIBarMetricsLandscapePhone];