In one of my app, i m using custom navigation bar view to handle all Navigation Bar Properties.
Is it good practice to use custom navigation in given below way? Any Optimizations need to be done. It would be helpful to me, if you have any valuable suggestions.
Source Code ---->
.h
#import <UIKit/UIKit.h>
@protocol VCNavigationBarViewDelegate <NSObject>
@optional
- (void)menuButtonPressed;
- (void)backButtonPressed;
- (void)cancelButtonPressed;
- (void)filterButonApplyPressed;
- (void)NotificationButtonPressed;
@end
typedef enum {
NavigationStyleLeftMenu = 0,
NavigationStyleBackButton,
NavigationStyleCancelButton,
NavigationStyleSearch,
NavigationStyleFilter,
NavigationStyleEmpty,
NavigationStyleNotification
} NavigationStyle;
@interface VCNavigationBarView : UIView
@property (weak, nonatomic) IBOutlet UIImageView *imgLogoConnect;
@property (weak, nonatomic) IBOutlet UIButton *btnMenu;
@property (weak, nonatomic) IBOutlet UIButton *btnBack;
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
@property (weak, nonatomic) IBOutlet UIButton *btnFilterApply;
@property (weak, nonatomic) IBOutlet UIButton *btnFilterCancel;
@property (weak, nonatomic) IBOutlet UILabel *lblHeaderTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnNotification;
@property (weak, nonatomic) id <VCNavigationBarViewDelegate> navigationDelegate;
- (IBAction)btnBackAction:(id)sender;
- (IBAction)btnMenuAction:(id)sender;
- (IBAction)btnCancelction:(id)sender;
- (IBAction)btnFilterApplyAction:(id)sender;
- (IBAction)btnNotificationAction:(id)sender;
- (void)configureWithStyle:(NavigationStyle)navigationStyle;
@end
.m
#import "VCNavigationBarView.h"
@implementation VCNavigationBarView
- (void)configureWithStyle:(NavigationStyle)navigationStyle
{
self.btnFilterApply.hidden = YES;
self.btnFilterCancel.hidden = YES;
self.btnBack.hidden = YES;
self.btnCancel.hidden = YES;
self.btnMenu.hidden = YES;
self.lblHeaderTitle.hidden = YES;
self.btnNotification.hidden = YES;
self.imgLogoConnect.hidden = YES;
switch(navigationStyle)
{
case NavigationStyleLeftMenu:
{
self.btnMenu.hidden = NO;
self.btnNotification.hidden = NO;
self.imgLogoConnect.hidden = NO;
}
break;
case NavigationStyleBackButton:
{
self.lblHeaderTitle.hidden = NO;
self.btnBack.hidden = NO;
}
break;
case NavigationStyleCancelButton:
{
self.lblHeaderTitle.hidden = NO;
self.btnCancel.hidden = NO;
}
break;
case NavigationStyleSearch:
{
self.btnCancel.hidden = NO;
}
break;
case NavigationStyleFilter:
{
self.lblHeaderTitle.hidden = NO;
self.btnFilterApply.hidden = NO;
self.btnFilterCancel.hidden = NO;
self.backgroundColor = [UIColor colorWithRed:76.0/255 green:76.0/255 blue:76.0/255 alpha:1.0];
}
break;
case NavigationStyleEmpty:
{
self.lblHeaderTitle.hidden = NO;
}
break;
case NavigationStyleNotification:
{
self.lblHeaderTitle.hidden = NO;
self.btnBack.hidden = NO;
}
break;
}
}
- (IBAction)btnBackAction:(id)sender {
if ([self.navigationDelegate respondsToSelector:@selector(backButtonPressed)]) {
[self.navigationDelegate backButtonPressed];
}
}
- (IBAction)btnMenuAction:(id)sender{
if([self.navigationDelegate respondsToSelector:@selector(menuButtonPressed)])
[self.navigationDelegate menuButtonPressed];
}
- (IBAction)btnCancelction:(id)sender{
if([self.navigationDelegate respondsToSelector:@selector(cancelButtonPressed)])
[self.navigationDelegate cancelButtonPressed];
}
- (IBAction)btnFilterApplyAction:(id)sender{
if([self.navigationDelegate respondsToSelector:@selector(filterButonApplyPressed)])
[self.navigationDelegate filterButonApplyPressed];
}
- (IBAction)btnNotificationAction:(id)sender{
if([self.navigationDelegate respondsToSelector:@selector(NotificationButtonPressed)])
[self.navigationDelegate NotificationButtonPressed];
}
@end
In BaseViewController, All Other View Controller inherited from BaseViewController
BaseVC.h
@interface VCBaseViewController : UIViewController<VCNavigationBarViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navigationBarView;
- (void)configureNavigationBarWithStyle:(NavigationStyle)style;
- (void)setViewControllerTitle:(NSString *) title;
BaseVC.m
navigationBar Configuraion
- (void)configureNavigationBarWithStyle:(NavigationStyle)style
{
CGRect frame;
NSArray *nibItems = [[NSBundle mainBundle] loadNibNamed:@"VCNavigationBarView" owner:nil options:nil];
self.menuNavigationBarView = [nibItems firstObject];
self.menuNavigationBarView.navigationDelegate = self;
[self.menuNavigationBarView configureWithStyle:style];
frame = self.navigationBarView.frame;
frame.origin.y = 20.0;
self.navigationBarView.frame = frame;
[self.navigationBarView addSubview:self.menuNavigationBarView];
NSArray *subConstraints;
NSDictionary *viewsDictionary = @{@"menuNavigationBarView" : self.menuNavigationBarView };
self.menuNavigationBarView.translatesAutoresizingMaskIntoConstraints = NO;
// Set left & right constraints to the container view
subConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[menuNavigationBarView]|"
options:0
metrics:nil
views:viewsDictionary];
[self.navigationBarView addConstraints:subConstraints];
// Set left & right constraints to the container view
subConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[menuNavigationBarView]|"
options:0
metrics:nil
views:viewsDictionary];
[self.navigationBarView addConstraints:subConstraints];
}
- (void) setViewControllerTitle:(NSString *) title
{
[self.menuNavigationBarView.lblHeaderTitle setText:title];
}
In Child/Inherited View Controller from BaseViewController
.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureNavigationBarWithStyle:NavigationStyleBackButton];
[self setViewControllerTitle:@"My Orders"];
}