它适用于没有标签栏但不适用于标签栏的 ViewController。此外,这是故事板中的第一个视图控制器。
UIImage *bookmarkImage = [UIImage imageNamed:@"bookmark"];
//create the button and assign the image
UIButton *bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
//set the frame of the button to the size of the image (see note below)
bookmarkButton.frame = CGRectMake(100, 0, bookmarkImage.size.width, bookmarkImage.size.height);
[bookmarkButton setImage:bookmarkImage forState:UIControlStateNormal];
bookmarkButton addTarget:self action:@selector(addFavorite:) forControlEvents:UIControlEventTouchUpInside];
bookmarkButton.imageView.contentMode=UIViewContentModeScaleAspectFit;
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem2 = [[UIBarButtonItem alloc] initWithCustomView:bookmarkButton];
self.navigationItem.rightBarButtonItem = customBarItem2;