태그 보관물: xcode

xcode

UINavigationBar에 버튼을 추가하는 방법은 무엇입니까?

프로그래밍 방식으로 UINavigationBar에 단추를 추가하는 방법은 무엇입니까?



답변

샘플 코드를 설정 rightbutton켜짐 NavigationBar.

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[bar pushNavigationItem:item animated:NO];

그러나 일반적으로 다음 NavigationController과 같이 작성할 수있는.

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;

답변

위의 답변은 좋지만 몇 가지 팁을 추가하여 구체화하고 싶습니다.

뒤로 버튼의 제목 (내비게이션 바의 왼쪽에있는 화살표 모양)을 수정하려면 반드시 표시되는 것이 아닌 이전보기 컨트롤러에서 수정해야합니다. 마치 “이것 위에 다른 뷰 컨트롤러를 밀어 넣은 적이 있다면 기본값 대신 뒤로 버튼을”뒤로 “라고 부르는 것과 같습니다.”

UIPickerView가 표시되는 동안과 같이 특수한 상태에서 뒤로 버튼을 숨기려면 특수 상태에서 self.navigationItem.hidesBackButton = YES;나갈 때 다시 설정해야합니다.

특수 기호 단추 중 하나를 표시하려면 다음 initWithBarButtonSystemItem:target:action과 같은 값으로 양식 을 사용하십시오.UIBarButtonSystemItemAdd

해당 기호의 의미는 귀하에게 달려 있지만 휴먼 인터페이스 지침에주의하십시오. UIBarButtonSystemItemAdd를 사용하여 항목을 삭제하면 응용 프로그램이 거부 될 수 있습니다.


답변

내비게이션 바에 사용자 정의 버튼 추가 (buttonItem 이미지 포함 및 작업 메소드 지정 (void) openView {} 및).

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 32, 32);
[button setImage:[UIImage imageNamed:@"settings_b.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
[barButton setCustomView:button];
self.navigationItem.rightBarButtonItem=barButton;

[button release];
[barButton release];

답변

아래 예에서는 오른쪽 탐색 모음에 제목이 “연락처”인 단추를 표시합니다. 이 작업은 viewcontroller에서 “contact”라는 메서드를 호출합니다. 이 라인이 없으면 오른쪽 버튼이 보이지 않습니다.

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact"
                                                                          style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];;


답변

Swift 2에서는 다음을 수행합니다.

let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = rightButton

(주요 변경 사항은 아님) Swift 4/5에서는 다음과 같습니다.

let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = rightButton

답변

다음을 사용하지 않는 이유 : ( iPhone 탐색 막대의 사용자 지정 뒤로 그리기 단추에서 )

// Add left
UINavigationItem *previousItem = [[UINavigationItem alloc] initWithTitle:@"Back title"];
UINavigationItem *currentItem = [[UINavigationItem alloc] initWithTitle:@"Main Title"];
[self.navigationController.navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil] animated:YES];

// set the delegate to self
[self.navigationController.navigationBar setDelegate:self];

답변

스위프트 3

    let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(cancelPressed(_:)))
    cancelBarButton.setTitleTextAttributes( [NSFontAttributeName : UIFont.cancelBarButtonFont(),
                                                          NSForegroundColorAttributeName : UIColor.white], for: .normal)
    self.navigationItem.leftBarButtonItem = cancelBarButton


    func cancelPressed(_ sender: UIBarButtonItem ) {
        self.dismiss(animated: true, completion: nil)
    }