有谁能告诉我为什么这段代码不起作用?
self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.backButton setImage:[UIImage imageNamed:@"back_arrow.png"]
forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
action:@selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
[navigationItem setLeftBarButtonItem:backButtonItem animated:NO];
navigationItem.hidesBackButton = YES;
编辑:
leftBarButtonItem
上没有任何内容。这就是问题所在。
答案 0 :(得分:3)
这应该有效
CGRect rect = CGRectMake(10, 0, 30, 30);
self.backButton = [[UIButton alloc] initWithFrame:rect];
[self.backButton setImage:[UIImage imageNamed:@"back_arrow.png"]
forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
action:@selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
self.navigationItem.hidesBackButton = YES;
答案 1 :(得分:2)
来自文档:
“创建自定义按钮时 - 这是一个类型为UIButtonTypeCustom的按钮 - 按钮的框架最初设置为(0,0,0,0)。在将按钮添加到界面之前,您应该更新框架到更合适的价值。“
所以如果你在第2行设置框架,你应该看到一些东西,例如:
self.backButton.frame = CGRectMake(0, 0, 40, 20);