廢話不多說,直接正文,有時候我們想要在導航欄實現多個Item,所以就用到了UIToolBar。
navigationItem可以定義兩個地方,rightBarButtonItem與leftBarButtonItem。
例如,我們要實現這個導航欄樣式:
具體代碼:
UIToolbar*tools=[[UIToolbar alloc]initWithFrame:CGRectMake(5, 0, 80, 39)]; //解決出現的那條線 tools.clipsToBounds = YES; //解決tools背景顏色的問題 [tools setBackgroundImage:[UIImage new]forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; [tools setShadowImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny]; //添加兩個button NSMutableArray*buttons=[[NSMutableArray alloc]initWithCapacity:2]; UIBarButtonItem*button3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"你的圖片"] style: UIBarButtonItemStyleDone target:self action:@selector(press2)]; UIBarButtonItem*button2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"你的圖片"] style: UIBarButtonItemStyleDone target:self action:@selector(press)]; button3.tintColor=[UIColor whiteColor]; button2.tintColor=[UIColor whiteColor]; [buttons addObject:button3]; [buttons addObject:button2]; [tools setItems:buttons animated:NO]; UIBarButtonItem*btn=[[UIBarButtonItem alloc]initWithCustomView:tools]; self.navigationItem.rightBarButtonItem=btn;