動態添加的 UIView 遮擋 UIButton,在 viewDidLoad 調用 bringSubviewToFront: 無效,因後續添加的 View 會蓋在按鈕之上。解決方法:將按鈕插入到極高的層級索引,或在添加新 View 時指定插入位置。
[self.view insertSubview:buttonView atIndex:1000]; // 足夠大的索引確保最上層
// 或
[self.view insertSubview:buttonView atIndex:self.view.subviews.count]; // 當前最上層
// 添加動態 View 時
[self.view insertSubview:dynamicView belowSubview:buttonView];
// 或
[self.view insertSubview:dynamicView atIndex:[self.view.subviews indexOfObject:buttonView]];
// 容器層級:背景容器 -> 內容容器 -> 按鈕容器
UIView *contentContainer = [[UIView alloc] initWithFrame:self.view.bounds];
UIView *buttonContainer = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:contentContainer];
[self.view addSubview:buttonContainer];
[buttonContainer addSubview:button]; // 按鈕永遠在最上層容器內
// 動態 View 只加入 contentContainer
subviews 數組順序決定繪製順序,索引越大越上層。bringSubviewToFront: 只是將 View 移到數組末尾,後續 addSubview: 會再次蓋住它。insertSubview:atIndex: 或 belowSubview: 精確控制相對層級。掌握視圖層級管理,按鈕永不被遮擋。