我加入一個UISegmentedControl正下方導航欄在一個UITableViewController下添加一個UISegmentedControl。這是代碼。導航欄
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationBar = self.navigationController.navigationBar;
UIView *segmentView=[[UIView alloc] initWithFrame:CGRectMake(0, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, 50)];
[segmentView setBackgroundColor:[UIColor whiteColor]];
segmentView.alpha = 0.95;
self.tabSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Favourites", @"All", nil]];
self.tabSegmentedControl.frame = CGRectMake(20, 10, self.navigationBar.frame.size.width - 40, 30);
[self.tabSegmentedControl addTarget:self action:@selector(tabChanged:) forControlEvents:UIControlEventValueChanged];
[segmentView addSubview:self.tabSegmentedControl];
[self.navigationBar addSubview:segmentView];
[self.tabSegmentedControl setSelectedSegmentIndex:1];
}
視圖和SegmentedControl出現在屏幕上,但它們不可點擊。點擊SegmentControl時,選擇器不會執行;它甚至不切換標籤!事實上,當你敲打那就是下面的segmentView
(TableView中的項目)的東西,獲得點擊。我已經嘗試過,但未能理解爲什麼會發生這種情況!任何的意見都將會有幫助!
爲什麼要將segmentView作爲導航欄的子視圖添加?我認爲它看起來總是在正確的地方,因爲你已經正確設置了它的框架。但不知道它應該是導航欄的子視圖... –
我想整個事情是導航欄的一部分。我嘗試將它添加爲self.view的子視圖,但是會將整個segmentView與表一起移動。我希望它不要移動。 –
所以你希望分段控制成爲導航欄的一部分,但在導航欄的標題/按鈕下面? –