self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"侧栏" style:UIBarButtonItemStylePlain target:self action:@selector(clickleftButton:)];
如果我们使用这行代码来定义按钮,无论左边右边,图片还是文字,他都会把字体或图片颜色变成默认的蓝色,然而并不是我们的需求,
所以我们这样
//导航栏 double按钮 //图片 UIImage *doubleButtonImage = [UIImage imageNamed:@"tx"]; //按钮 UIButton *doubleButton = [UIButton buttonWithType:UIButtonTypeCustom]; doubleButton.bounds = CGRectMake(0, 0, 30, 30); [doubleButton setImage:doubleButtonImage forState:UIControlStateNormal]; //按钮的点击事件 [doubleButton addTarget:self action:@selector(doubleButtonClick) forControlEvents:UIControlEventTouchUpInside]; //导航栏按钮 UIBarButtonItem *doubleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:doubleButton]; //添加到导航栏左边 self.navigationItem.leftBarButtonItem = doubleButtonItem;
自定义的按钮作为导航栏的按钮,重点是红色标记处,我们需要把按钮的类型设为custom而不是system,这样我们的按钮图片颜色就是本来的颜色了,如果是文字,颜色也可以自己设置下就搞定了,而不会是默认的蓝色了~
时间: 2024-12-28 21:32:53