谁能告诉我为什么此代码不起作用?

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上没有任何内容。那就是问题所在。

最佳答案

从文档:

“创建自定义按钮(即类型为UIButtonTypeCustom的按钮时,按钮的框架最初设置为(0,0,0,0)。在将按钮添加到界面之前,应将框架更新为更合适的价值。”

因此,如果在第2行中设置框架,您应该会看到一些东西,例如:

self.backButton.frame = CGRectMake(0, 0, 40, 20);

关于iphone - 将自定义按钮添加到UINavigationItem LeftBarButtonItem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13903939/

10-09 07:44