gpt4 book ai didi

iphone - 将自定义按钮添加到 UINavigationItem LeftBarButtonItem

转载 作者:行者123 更新时间:2023-12-01 17:58:10 33 4
gpt4 key购买 nike

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

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

最佳答案

从文档:

"When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value."



因此,如果您在第 2 行设置框架,您应该会看到一些内容,例如:
self.backButton.frame = CGRectMake(0, 0, 40, 20);

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

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com