为导航栏制作自定义后退button
我为我的导航栏创build了至less10个button,但它似乎从来没有工作正确。 圆angular边缘像素化。 我不能在一个应用程序,所以任何人都可以告诉我如何做一个像苹果一个好的图标? 另外什么是适当的大小? 该button的应用程序中的代码是
UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom]; backbtn.frame = CGRectMake(0, 0, 55, 30); [backbtn setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; [backbtn addTarget:self action:@selector(goBackOne) forControlEvents:UIControlEventTouchUpInside]; forState:UIControlStateNormal ]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];//set new button self.navigationItem.hidesBackButton = YES;//hide original back button
试试这个代码:
UIImage *backButtonImage = [UIImage imageNamed:@"backButton.png"]; UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [backButton setImage:backButtonImage forState:UIControlStateNormal]; backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height); [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.navigationItem.leftBarButtonItem = customBackBarItem;
和后面的方法:
-(void)back { [self.navigationController popViewControllerAnimated:YES]; }
上面的代码只是将图像设置为正常状态。 您还可以设置突出显示的状态以获得更好的外观。 添加一些代码像:
UIImage *backButtonImageHighlighted = [UIImage imageNamed:@"backButtonHighlighted.png"]; [backButton setImage:backButtonImageHighlighted forState:UIControlStateHighlighted];