簡體   English   中英

使用界面生成器自定義UIBarButtonItem

[英]Custom UIBarButtonItem with interface builder

我在A UIViewController中使用pushViewController:animated:方法轉到B UIViewController,並且兩個UIViewController具有self xib文件。

在系統默認情況下,我不需要更改任何內容,可以從B UIViewController返回A UIViewController。

但是我想自定義我的后退按鈕,所以我使用如下代碼所示的解決方案:

- (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];
}

就像這樣,但是我想在界面生成器中更改它以修改xib文件來執行此操作。 我嘗試像這樣使用:

在此處輸入圖片說明

但是系統默認導航欄是封面,當我隱藏系統默認欄時,滑動手勢(像彈出操作一樣返回)也將被刪除。

有什么辦法可以做我想要的嗎?

順便說一句,我在自動生成的界面生成器中使用了導航欄,如何讓它看起來像這樣的系統默認導航欄(填補狀態欄的空白)?

在此處輸入圖片說明

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)

如果使用情節提要,則只需取消選中顯示導航欄。

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