xcode 使用“完成”按钮以编程方式添加 UINavigationBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14545398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:38:32  来源:igfitidea点击:

Add UINavigationBar programmatically with a Done button

objective-cxcodeuinavigationbarmodalviewcontroller

提问by iPwnTech

So I have a modal view and want to add a UINavigationBar programmatically with a Done button to dismiss this view when the user finishes reading the content.

所以我有一个模态视图,并希望以编程方式添加一个带有完成按钮的 UINavigationBar 以在用户完成阅读内容时关闭此视图。

Any ideas on how to do this and if it's possible purely without using the interface builder?

关于如何做到这一点的任何想法,以及是否可以完全不使用界面构建器?

回答by codeRefiner

I am sorry that nobody here actually read your question... here is the code you are looking for:

很抱歉,这里没有人真正阅读您的问题......这是您正在寻找的代码:

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

UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.title = @"Navigation Bar title here";

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(yourMethod:)];
navItem.leftBarButtonItem = leftButton;

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Post" style:UIBarButtonItemStylePlain target:self action:@selector(yourOtherMethod:)];
navItem.rightBarButtonItem = rightButton;

navBar.items = @[ navItem ];

[self.view addSubview:navBar];

I hope this helps, good luck :)

我希望这会有所帮助,祝你好运:)

Add this code to your viewDidLoad method and everything will construct itself. Mind you, replace the selectors with your own method signatures-

将此代码添加到您的 viewDidLoad 方法中,一切都会自行构建。请注意,用您自己的方法签名替换选择器-

Happy coding

快乐编码

回答by Gabriele Petronella

It is definitely possible.

这绝对是可能的。

Probably the easiest way is to embed the UIViewControlleryour are presenting modally into a UINavigationViewControllerand then add the Donebutton, doing something like

可能最简单的方法是将UIViewController您以模态方式呈现的内容嵌入到 a 中UINavigationViewController,然后添加Done按钮,执行类似的操作

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] 
                            initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                            target:self 
                            action:@selector(dismiss)];
self.navigationItem.rightBarButtonItem = doneButton;

and implement a dismissmethod like follows

并实现dismiss如下方法

- (void)dismiss {
    [self.presentingViewController dismissViewControllerAnimated:YES 
              ?????                completion:nil];
}

回答by iPatel

//add done button to navigation bar
UIBarButtonItem *doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(userPressedDone)];
self.navigationItem.rightBarButtonItem = doneBarButtonItem;

Then have a method like this somewhere in your view controller

然后在你的视图控制器的某个地方有一个这样的方法

-(void)userPressedDone {
    // Action For Done Button Tapped
}