我在名为login的项目中有一个标签栏按钮。我想为此分配自定义图像,这怎么可能?

我正在按照以下方式操作,但应用程序崩溃。

这是代码:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(loginPressed)];
self.navigationItem.leftBarButtonItem = nil;
[self.navigationItem.rightBarButtonItem setBackgroundImage:[UIImage imageNamed:@"loginN.png"] f

最佳答案

尝试这种方式:

UIImage *image = [UIImage imageNamed:@"facebook.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(loginPressed) forControlEvents:UIControlEventTouchDragInside];
UIBarButtonItem *rbBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = rbBtn;

关于ios - 如何在ipad中为UIBarButtonitem设置图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17294675/

10-14 23:21