Iphone 将项目添加到导航栏(不使用UINavigationController)

Iphone 将项目添加到导航栏(不使用UINavigationController),iphone,cocoa-touch,Iphone,Cocoa Touch,我有一个包含UITableView的UIViewController,还添加了一个UINavigationBar。 如何以编程方式在该栏中添加和“编辑”按钮以及“+”按钮? (我尝试过使用IB,但标题总是被替换,并且没有添加其他项目) 我没有使用UINavigationController。是我的UIViewController一个人站着 这就是我尝试过但没有成功的方法: UIBarButtonItem *barButton = [[UIBarButtonItem alloc] ini

我有一个包含UITableView的UIViewController,还添加了一个UINavigationBar。 如何以编程方式在该栏中添加和“编辑”按钮以及“+”按钮? (我尝试过使用IB,但标题总是被替换,并且没有添加其他项目) 我没有使用UINavigationController。是我的UIViewController一个人站着

这就是我尝试过但没有成功的方法:

UIBarButtonItem *barButton = 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered 
                                                  target:nil
                                                  action:nil];
UINavigationItem *editItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];

您的
UIViewController
具有
navigationItem
属性。您可以使用
self.navigationItem.LeftBarButtonim=…
self.navigationItem.RightBarButtonim=…
设置左右栏按钮项

编辑:

好的,我假设您有一个对
UINavigationBar
的引用? 那么我想您应该添加一个
UINavigationItem

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable
然后设置该项目的左右按钮:

theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;

我没有尝试过这个,但我认为它应该可以工作。

您不需要将UINavigationItem添加到UINavigationBar。您可以按照以下示例进行操作:

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(theEditMethod:)];      
[viewController.navigationItem setLeftBarButtonItem:leftBarButton animated:NO];
[leftBarButton release];

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(theAddMethod:)];       
[viewController.navigationItem setLeftBarButtonItem:rightBarButton animated:NO];
[rightBarButton release];
NSString *backButtonTittle=[NSString stringWithFormat:@"< %@",NSLocalizedString(@"backButton", nil)];
UIBarButtonItem *backCreateAccountNavBarItem=[[UIBarButtonItem alloc]initWithTitle:backButtonTittle style:UIBarButtonItemStylePlain target:self action:@selector(goToBackStep)];
self.createAccountNavBar.topItem.leftBarButtonItem=backCreateAccountNavBarItem;
NSString*backbuttonittle=[NSString stringWithFormat:@“<%@”,NSLocalizedString(@“backButton”,nil)];
UIBarButtonItem*backCreateAccountNavBarItem=[[UIBarButtonItem alloc]initWithTitle:BackbuttonItem样式:UIBarbuttonItems样式普通目标:自我操作:@selector(goToBackStep)];
self.createAccountNavBar.topItem.LeftBarButtonim=backCreateAccountNavBarItem;

只需使用viewController的navigationItem属性即可

像这样:

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(updateContent)];

在Swift 4.2、ios 11、XCode 10上具有操作的导航栏项目/按钮

1)使用情节提要进入编辑器>嵌入>导航栏

2)在AppDelegate>中,使用选项完成启动:

UINavigationBar.appearance().barTintColor = UIColor(hexString: "1C9B90")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
3)viewDidLoad上,为您的viewcontroller创建按钮并添加到导航栏:

self.navigationController?.navigationBar.topItem?.title = "Title"
self.navigationController?.isNavigationBarHidden = false

//QR Code button
let qrCodeScanButton = UIButton(type: .custom)
qrCodeScanButton.setImage(UIImage(named: "camera"), for: .normal)
qrCodeScanButton.addTarget(self, action: #selector(self.searchWithQRCode), for: .touchUpInside)
let qrCodeScanButtonItem = UIBarButtonItem(customView: qrCodeScanButton)

///LogOut button
let logoutButton = UIButton(type: .custom)
logoutButton.setImage(UIImage(named: "logOut"), for: .normal)
logoutButton.addTarget(self, action: #selector(self.logOut), for: .touchUpInside)
let logoutButtonItem = UIBarButtonItem(customView: logoutButton)

self.navigationController?.navigationBar.topItem?.setRightBarButtonItems([logoutButtonItem, qrCodeScanButtonItem], animated: true)
4)按钮操作:

@objc func logout(){
     ///present your Login VC
}

@objc func qrCodeScanButton(){
     ///present your Login VC
}
5)构建并运行

请记住这两个区别

isNavigationBarHidden:指示导航栏是否隐藏的布尔值

self.navigationController?.isNavigationBarHidden
导航栏。isHidden:由导航控制器管理的导航栏

self.navigationController?.navigationBar.isHidden

我尝试了控制器的viewDidLoad方法(并将viewController替换为self),但不起作用;(.navigationItem旨在在使用UINavigationController时使用,这不是我的情况。为什么不切换到使用导航控制器,并将视图控制器设置为其根目录。这将使您的生活更轻松!因为,首先,我不导航。其次,我想自定义其宽度,但UINavigationCon无法实现某些功能troller。事实上,我正在研究另一种导航方式,所以目标不是使用UINavigationController。记住,UINavigationController是UIViewController的子类,它的UIView中添加了UINavigationBar。如果您想创建自己的导航方式,可以使用UINavigationController来实现,但不要这样做t使用推送和弹出视图控制器。:)。如果您不想使用默认的导航结构,我建议您使用UIToolbar而不是UINavigationBar。这与vfn的方法相同,对吗?我没有工作。navigationItem打算在使用UINavigationController时使用,这不是我的情况。是的,这是相同的方法。我编辑了我的答案,并添加了另一个可能的解决方案。非常感谢,您的答案非常安全,节省了很多时间,特别是您做出了最新的假设,即您引用的是UINavigationBar,而不是NavigationItem,非常感谢:)