0

이미지와 텍스트를 모두 포함하는 사용자 지정 UIBarButtonItem을 만들고 UIButton 탭 영역을 사용자 지정 (줄이기)하고 싶습니다. 이 프로그래밍 방식으로 달성하려면 다음 코드 조각을 사용하고 있습니다. UIView에서 하위보기로 추가하면 UIButton이 사라집니다. 누군가 나에게 무엇이 잘못되었는지 안내 할 수 있습니까? 바 버튼의 탭 영역을 줄이기 위해 사용자 정의 UIView에 사용자 정의 UIButton을 임베드합니다.customView를 사용할 때 UINavigationBarButton이 작동하지 않습니다.

//Set Navigation Bar 
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; 

//Set title if needed 
UINavigationItem * navTitle = [[UINavigationItem alloc] init]; 
navTitle.title = @""; 
UIView *customBackView = [[UIView alloc] initWithFrame:CGRectMake(220,0,50,40)]; 
customBackView.backgroundColor = [UIColor clearColor]; 

//Here you create info button and customize it 
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
tempButton.frame=CGRectMake(240,2,40,40); 
[tempButton setImage:[UIImage imageNamed:@"offline_bk.png"] 
     forState:UIControlStateNormal]; 

//Add selector to info button 
[tempButton addTarget:self action:@selector(onTapDone:) forControlEvents:UIControlEventTouchUpInside]; 
[customBackView addSubview:tempButton]; 
[customBackView bringSubviewToFront:tempButton]; 
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:customBackView]; 

//In this case your button will be on the right side 
navTitle.rightBarButtonItem = infoButton; 

//Add NavigationBar on main view 
navBar.items = @[navTitle]; 
[self.view addSubview:navBar]; 

답변

1

임시 단추가 맞춤형 뒤로보기 범위를 벗어나기 때문입니다. y 원점 값을 2로 설정하고 x 원점 값을 240으로 설정하면 단추가 사용자 정의 뒤로보기 범위 밖으로 나옵니다. 버튼은 오른쪽 모서리에있는 경우, 우리는 식별 할 수없는 답장을

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; 

    //Set title if needed 
    UINavigationItem * navTitle = [[UINavigationItem alloc] init]; 
    navTitle.title = @""; 
    UIView *customBackView = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,50)]; 
    customBackView.backgroundColor = [UIColor clearColor]; 

    //Here you create info button and customize it 
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
    tempButton.frame=CGRectMake(4,2,40,40); 
    [tempButton setImage:[UIImage imageNamed:@"offline_bk.png"] 
       forState:UIControlStateNormal]; 

    //Add selector to info button 
    [tempButton addTarget:self action:@selector(onTapDone:) forControlEvents:UIControlEventTouchUpInside]; 
    [customBackView addSubview:tempButton]; 
    [customBackView bringSubviewToFront:tempButton]; 
    UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:customBackView]; 

    //In this case your button will be on the right side 
    navTitle.rightBarButtonItem = infoButton; 

    //Add NavigationBar on main view 
    navBar.items = @[navTitle]; 
    [self.view addSubview:navBar]; 
    [self.view bringSubviewToFront:navBar]; 
+0

감사합니다,하지만있는 UIButton 이미지를 볼 수 없습니다 :

이 코드를 사용해보십시오. – Gurp321

+0

당신은 할 수 있습니다. 뷰 디버거를 사용하여 버튼의 존재에 대한 뷰 계층 구조를 탐색 할 수 있습니다. 이미지의 크기에 문제가있을 수 있습니다. 버튼의 크기에 따라 이미지 파일을 작성하십시오. –

+0

더미 이미지로 시도해보고 있습니다. 사용중인 해상도가 정확하고 프로젝트 번들에 올바르게 추가되었습니다. –