Set a custom subclass of UINavigationBar in UINavigationController programmatically

前端 未结 12 1537
清歌不尽
清歌不尽 2020-11-28 17:29

Does anyone know how can I use my custom subclass of UINavigationBar if I instantiate UINavigationController programmatically (without IB)?

<
12条回答
  •  悲哀的现实
    2020-11-28 18:09

    As of iOS 4, you can use the UINib class to help solve this issue.

    1. Create your custom UINavigationBar subclass.
    2. Create an empty xib, add a UINavigationController as the single object.
    3. Set the class for the UINavigationController's UINavigationBar to your custom subclass.
    4. Set your root view controller via one of these methods:
      • [navController setViewcontrollers[NSArray arrayWithObject:myRootVC]];
      • [navController pushViewController:myRootVC];

    In code:

    UINib *nib = [UINib nibWithNibName:@"YourCustomXib" bundle:nil];
    UINavigationController *navController = 
                 [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
    


    Now you've got a UINavigationController with your custom UINavigationBar.

提交回复
热议问题