系統自帶的 UINavigationBar 定制能力有限,當設計稿要求「導航欄半透明、能看到底下內容的模糊色塊」時,自己封裝一個更好用。本文給出一份完整實現,核心只有兩步:用 UIVisualEffectView 鋪一層毛玻璃,再往上擺返回按鈕、標題和右側按鈕。
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#import "RSwenNav.h"
@implementation RSwenNav
{
UIVisualEffectView *effectview;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 第一步:鋪一層毛玻璃作為背景
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
effectview.frame = frame;
[self addSubview:effectview];
// 第二步:往上擺子控件
[self addSubviews];
}
return self;
}
#pragma mark - 佈局
- (void)addSubviews
{
[self addSubview:self.backBtn];
[self addSubview:self.titleLabel];
[self addSubview:self.rightBtn];
}
- (UIButton *)backBtn
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 20, 60, 44);
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(RSwenNavback) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
- (UILabel *)titleLabel
{
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:17];
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(100, 20, kScreenWidth - 200, 44);
label.text = @"我的主頁";
return label;
}
- (UIButton *)rightBtn
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 20, 80, 44);
[btn setTitle:@"保存" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
#pragma mark - 事件處理
// 返回上一頁
- (void)RSwenNavback
{
// 沿響應者鏈向上找到所屬的 UIViewController
id object = [self nextResponder];
while (![object isKindOfClass:[UIViewController class]] && object != nil) {
object = [object nextResponder];
}
UIViewController *uc = (UIViewController *)object;
[uc.navigationController popViewControllerAnimated:YES];
}
// 右側按鈕點擊,拋給外部代理處理
- (void)rightBtnClicked
{
if ([_delegate respondsToSelector:@selector(RSwenNavrightBtnClicked)]) {
[_delegate RSwenNavrightBtnClicked];
}
}
@end
1. 毛玻璃的實現方式。iOS 8 之後,實現毛玻璃只需要三行:UIBlurEffect 描述模糊風格(Light / Dark / ExtraLight,iOS 10 起還多了多種 Prominent 與 Regular 變體),UIVisualEffectView 把它渲染出來,最後加到視圖層級的最底部。這個方案由系統合成,性能遠優於早期「截圖 + 高斯模糊」的做法。
2. 層級順序決定視覺結果。effectview 必須最先 addSubview,之後添加的按鈕和標題才會浮在模糊層之上。順序顛倒的話,模糊層會把控件糊掉。
3. 用響應者鏈找所屬的控制器。自定義 view 通常不持有 controller 引用,直接 popViewControllerAnimated: 無從下手。這裡用 nextResponder 沿鏈上溯,直到命中 UIViewController。這種寫法的好處是視圖保持獨立、可複用;缺點是依賴視圖層級結構,若把導航欄放進某個容器 view 裡,循環仍能在有限層數內找到 controller,但要注意 while 的 nil 判斷不能少,否則會死循環。
4. 右側按鈕回報給外部。用 delegate 而不是直接操作,讓這個導航欄保持通用——「保存」按鈕點下去到底做什麼,由使用方決定。判斷 respondsToSelector: 是必要的防護,避免未設置 delegate 時崩潰。
這份代碼用固定 frame 硬編碼佈局,實際項目建議改為 Auto Layout(或至少傳入 topInset 以適配瀏海屏與不同狀態欄高度)。此外標題文字目前是通過 text 直接賦值的,若需要支持異步加載或長標題截斷,可把 titleLabel.text 開成公開屬性,或提供一個 updateTitle: 方法給外部調用。