UINavigationBar
XIB없이에 2 개의 버튼을 추가하려면 어떻게 해야합니까?
2 개의 버튼은 UINavigationBar
.
하나의 버튼을 추가하는 방법을 알고 있지만 두 개는 어떻습니까?
답변
iOS 5 이상에서는 다음과 같이 쉽습니다.
UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share)];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:btnShare, btnRefresh, nil]];
답변
내가 코드를 게시 내비게이션 바의 오른쪽에 두 개의 버튼을 추가합니다 (아래 참조). barStyle = -1
서브 클래 싱 대신 설정할 수 있습니다 UIToolbar
.
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = NO;
tools.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; // closest I could get by eye to black, translucent style.
// anyone know how to get it perfect?
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create a standard refresh button.
UIBarButtonItem *bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
[buttons addObject:bi];
[bi release];
// Create a spacer.
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width = 12.0f;
[buttons addObject:bi];
[bi release];
// Add profile button.
bi = [[UIBarButtonItem alloc] initWithTitle:@"Profile" style:UIBarButtonItemStylePlain target:self action:@selector(goToProfile)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
답변
도구 모음 으로 초기화 된 막대 단추를 사용자 정의보기로 사용할 수 있습니다 .
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
NSArray* buttons = [NSArray arrayWithObjects:self.editButtonItem, someOtherButton, nil];
[toolbar setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
다음과 같은 것을 줄 것입니다.
참고 :이 답변은 iOS 6 이상에서 사용되지 않습니다.
답변
다음은 현재 프로젝트의 작업 예입니다.
UIButton *homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[homeBtn setImage:[UIImage imageNamed:@"home-icon"] forState:UIControlStateNormal];
//[homeBtn addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside];
[homeBtn setFrame:CGRectMake(0, 0, 32, 32)];
UIButton *settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingsBtn setImage:[UIImage imageNamed:@"settings-icon"] forState:UIControlStateNormal];
//[settingsBtn addTarget:self action:@selector(settings) forControlEvents:UIControlEventTouchUpInside];
[settingsBtn setFrame:CGRectMake(44, 0, 32, 32)];
UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
[rightBarButtonItems addSubview:homeBtn];
[rightBarButtonItems addSubview:settingsBtn];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];
답변
UINavigationBar *navBarView = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 42.0f)];
navBarView.tintColor= [UIColor colorWithRed:90.0/255.0f green:53.0/255.0f blue:45.0/255.0f alpha:1.0];
UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backView)];
UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(SaveImage)];
UIBarButtonItem *Add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddComment:)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
navigationItem.leftBarButtonItem = left;
NSMutableArray *buttonArray=[[NSMutableArray alloc]initWithCapacity:2];
[buttonArray addObject:right];
[buttonArray addObject:Add];
navigationItem.rightBarButtonItems = buttonArray;
[navBarView pushNavigationItem:navigationItem animated:NO];
[self.view addSubView:navBarView];
답변
빠른:
override func viewDidLoad() {
super.viewDidLoad()
// Additional bar button items
let button1 = UIBarButtonItem(image: UIImage(named: "image1.png"), style: .Plain, target: self, action: "methodA")
let button2 = UIBarButtonItem(image: UIImage(named: "image2.png"), style: .Plain, target: self, action: "methodB")
let button3 = UIBarButtonItem(image: UIImage(named: "image3.png"), style: .Plain, target: self, action: "methodC")
navigationItem.leftItemsSupplementBackButton = true
navigationItem.setLeftBarButtonItem(button1, animated: true)
navigationItem.setRightBarButtonItems([button2, button3], animated: true)
버튼 방법 :
func methodA() {
performSegueWithIdentifier("segA", sender: nil)
}
func methodB() {
performSegueWithIdentifier("segB", sender: nil)
}
func methodC() {
performSegueWithIdentifier("segC", sender: nil)
}
답변
빠른
당신은에서 할 수있는 신속 같은
var editImg : UIImage = UIImage(named: "plus")!
var searchImg : UIImage = UIImage(named: "search")!
var editBtn : UIBarButtonItem = UIBarButtonItem(image: editImg, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editBtnPressed:"))
var searchBtn : UIBarButtonItem = UIBarButtonItem(image: searchImg, style: UIBarButtonItemStyle.Plain, target: self, action: Selector ("searchBtnPressed:"))
var buttons : NSArray = [editBtn, searchBtn]
self.navigationItem.rightBarButtonItems = buttons
func editBtnPressed(sender: AnyObject){
}
func searchBtnPressed(sender: AnyObject){
}