简体   繁体   English

使用界面生成器自定义UIBarButtonItem

[英]Custom UIBarButtonItem with interface builder

I used pushViewController:animated: method in A UIViewController to go to B UIViewController, and the two UIViewController had selfs xib file. 我在A UIViewController中使用pushViewController:animated:方法转到B UIViewController,并且两个UIViewController具有self xib文件。

In system default, I did not need to change anything and I can go back A UIViewController from B UIViewController. 在系统默认情况下,我不需要更改任何内容,可以从B UIViewController返回A UIViewController。

But I want to custom my back button, I use the solution like the follow code: 但是我想自定义我的后退按钮,所以我使用如下代码所示的解决方案:

- (void)initBarButton {
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];

    self.navigationItem.leftBarButtonItem = cancelButton;

    UIBarButtonItem *updateButton = [[UIBarButtonItem alloc] initWithTitle:@"Update" style:UIBarButtonItemStyleDone target:self action:@selector(save)];

    self.navigationItem.rightBarButtonItem = updateButton;
}

- (void)cancel {
    [self.navigationController popViewControllerAnimated:YES];
}

Just like this, but I want to change that in interface builder to modified the xib file to do this. 就像这样,但是我想在界面生成器中更改它以修改xib文件来执行此操作。 I try to use that like: 我尝试像这样使用:

在此处输入图片说明

But the system default navigation bar is cover, when I hide the system default bar, the swipe gesture(go back like pop action) also be removed. 但是系统默认导航栏是封面,当我隐藏系统默认栏时,滑动手势(像弹出操作一样返回)也将被删除。

Is any possible way to do what I want? 有什么办法可以做我想要的吗?

BTW, I used the navigation bar in interface builder with auto layout, how can I let it look like system default navigation bar like this(fill the status bar gap)? 顺便说一句,我在自动生成的界面生成器中使用了导航栏,如何让它看起来像这样的系统默认导航栏(填补状态栏的空白)?

在此处输入图片说明

Marcus Wu If you want to use XIB instead of storyboard for achieving this, you can see the below steps Marcus Wu如果您要使用XIB而不是情节提要来实现此目的,则可以查看以下步骤

1.Remove the storyboard from the project
2.Also delete the Main from Main Interface of Deployment Target of Project Target.
3.Now create the New User Interface(View or Empty) and give name as ViewController
4.Then click the Placeholder File Owner with right side Identity Inspector
5.In identity Inspector Custom Class -> Class ->Click the drop down and choose the ViewController.
6.Then Click right side connections inspector.
7.Click the empty circel(name is View) in Outlet.Drag this to the view.I mean you need to press control+emptycircle and drag it to the View. 

8.in appDelegate.h
   Import - #import "ViewController.h"
   @property (strong, nonatomic) ViewController *vc;

9.In appDelegate.m
   @synthesize vc;
   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
   {
      // Override point for customization after application launch.
      self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
      vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
      UINavigationController *navigationVC  = [[UINavigationController alloc]initWithRootViewController:vc];
      navigationVC.navigationBarHidden = YES;
      self.window.rootViewController = navigationVC;
      self.window.backgroundColor = [UIColor clearColor];
      [self.window makeKeyAndVisible];
      return YES;
   }

10.Now in xib create the custom button Back programmatically or from objects(drag button and give connection)

If you use storyboard,you just need to untick the the shows navigation bar. 如果使用情节提要,则只需取消选中显示导航栏。

1.First Click NavigationController in stroyboard
2.Click the Attributes Inspector of Right side
3.Then Click Navigation Controller
4.Inside the Navigation Controller 
     UnTick the Bar Visibility -  Shows Navigation Bar
5.Now create the custom button programmatically or drag it from the Object Library to ViewController in XIB 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM