我廠廣招各路大神加入:job.koudaitong.com
可以發簡歷到 tianchi@qima-inc.com O(∩_∩)O~
使用自定義UIView替換UINavigationBar,在ViewDidLoad中setNavigationBarHidden:YES;
自定義UIView的實現–CustomNavView
CustomNavView.h
#import <UIKit/UIKit.h>
@interface CustomNavView : UIView
@property (weak, nonatomic) IBOutlet UILabel *titleLb;
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (nonatomic,strong) UIButton *leftBtn;
@property (nonatomic,strong) UIView *titleView;
@property (nonatomic,strong) NSArray *rightBtnArr;
-(void)hideNavWithAnimation:(BOOL)isAnimation;
-(void)showNavWithAnimation:(BOOL)isAnimation;
@end
這裡對leftBtn只設定了一個,而rightBtn使用了陣列,支援了多個Btn,可以根據自己專案的需求使leftBtn支援多個按鈕。hide於show方法是為了支援專案中上滑隱藏於下滑顯示的效果,同樣可以用於navView的顯示與隱藏。titleView與titleLb有點重複了。我在專案中使用了XIB,使用純程式碼應該更好。
.m實現:
- (id)initWithFrame:(CGRect)frame{
self = [[[NSBundle mainBundle] loadNibNamed:@"CustomNavView" owner:self options:nil] objectAtIndex:0];
if (self) {
frame.size.height=IS_IOS_7?64:44;
self.frame=frame;
_rightBtnArr=[[NSArray alloc] init];
_leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
_titleView=[[UIView alloc] init];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.frame=CGRectMake(0,0,320,(IS_IOS_7?64:44));
}
在layout中把view的frame重新設定回來,防止再初始化的時候大小設定失誤(其實初始化的時候直接使用init方法就夠了,而且layout方法會被一直呼叫)。
-(void)hideNavWithAnimation:(BOOL)isAnimation{
[UIView animateWithDuration:0.4 animations:^{
self.frame=CGRectMake(0,-44,320,(IS_IOS_7?:64:44));
self.titleView.alpha=0;
self.leftBtn.alpha=0;
[self hideRightBtns];
}];
}
-(void)showNavWithAnimation:(BOOL)isAnimation{
[UIView animateWithDuration:0.4 animations:^{
self.frame=CGRectMake(0,0,320,(IS_IOS_7?:64:44));
self.titleView.alpha=1;
self.leftBtn.alpha=1;
[self showRightBtns];
}];
}
用0.4秒的動畫來hide與show。
-(void)hideRightBtns{
for (UIButton *btn in _rightBtnArr) {
btn.alpha=0;
}
}
-(void)showRightBtns{
for (UIButton *btn in _rightBtnArr) {
btn.alpha=1;
}
}
這裡沒有判斷陣列中是否真的是UIButton,不安全(- – 為什麼我沒有改,因為專案實在急著適配,又是一扇破窗……)
-(void)setRightBtnArr:(NSArray *)rightBtnArr{
if (_rightBtnArr.count>0) {
for (UIButton *btn in _rightBtnArr) {
[btn removeFromSuperview];
}
}
_rightBtnArr=rightBtnArr;
CGFloat xTag=0.0;
for (UIButton *btn in rightBtnArr) {
CGRect btnFrame=btn.frame;
btnFrame.origin.x=320-btn.frame.size.width-xTag;
if (IS_IOS_7) {
btnFrame.origin.y=(self.frame.size.height+20-_leftBtn.frame.size.height)/2;
}else{
btnFrame.origin.y=(self.frame.size.height-_leftBtn.frame.size.height)/2;
}
btn.frame=btnFrame;
[self addSubview:btn];
xTag=320-btnFrame.origin.x;
}
}
設定rightBtnArr的時候需要把原來的那些Btn從View上移除掉,不然就是各種疊加,然後再把新的Btn加上去。
-(void)setLeftBtn:(UIButton *)leftBtn{
_leftBtn=leftBtn;
CGRect btnFrame=_leftBtn.frame;
btnFrame.origin.x=0;
if (IS_IOS_7) {
btnFrame.origin.y=(self.frame.size.height+20-_leftBtn.frame.size.height)/2;
}else{
btnFrame.origin.y=(self.frame.size.height-_leftBtn.frame.size.height)/2;
}
_leftBtn.frame=btnFrame;
[self addSubview:_leftBtn];
}
-(void)setTitleView:(UIView *)titleView{
_titleView=titleView;
_titleView.center=CGPointMake(self.center.x, IS_IOS_7==YES?self.center.y+10:self.center.y);
[self addSubview:_titleView];
}
寫在最後:這只是一種應急的適配方案,能夠做到快速的進行適配,但是對於可擴充套件性、重用性與可維護性來說還是比較差的,打算再做一個自定義的UINavigationController這樣用起來應該更加方便!