我正在尝试使用自定义尺寸的导航栏开发应用。 (100px)在导航栏中,我还想要包含一个后退按钮。我已使用子视图添加了它们,但后退按钮不起作用:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"menueButton.png"];
UIImage *backBtnImagePressed = [UIImage imageNamed:@"menueButton.png"];
backBtn.exclusiveTouch = YES;
[backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn setBackgroundImage:backBtnImagePressed forState:UIControlStateHighlighted];
backBtn.frame = CGRectMake(0, -40, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
[backButtonView addSubview:backBtn];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = backButton;
我的导航栏尺寸为100像素高,当我使用按钮位置(0,0,35,32)时,它将正常工作。但在这种情况下,按钮显示的内容远低于预期。我做错了什么?
答案 0 :(得分:0)
尝试像这样分配backButtonView
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35 , 32)];
更改:视图的原点为(0,0)
编辑
尝试
backBtn.frame = CGRectMake(0, 0, 35, 32);
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, -40, 35 , 32)];
答案 1 :(得分:0)
self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setImage:[UIImage imageNamed:@"back_active.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
self.btnBack.frame = CGRectMake(5, 9, 50, 30);
[self.navigationController.navigationBar addSubview:self.btnBack];
同时检查此link。
更新:用于后退按钮
-(void)back
{
[self.btnBack removeFromSuperview];
[self.navigationController popViewControllerAnimated animated:NO];
}