Iphone IOS将右键添加到导航栏不工作
它适用于没有选项卡栏但没有选项卡栏的ViewController。 此外,这是故事板中的第一个视图控制器Iphone IOS将右键添加到导航栏不工作,iphone,objective-c,ios,Iphone,Objective C,Ios,它适用于没有选项卡栏但没有选项卡栏的ViewController。 此外,这是故事板中的第一个视图控制器 UIImage *bookmarkImage = [UIImage imageNamed:@"bookmark"]; //create the button and assign the image UIButton *bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom]; //set the frame of th
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;
我在理解您的问题时遇到了一些问题,但据我所知,您试图添加按钮的类似乎不在
UINavigationController
中
我不确定如何实例化这个类(ClassX),但应该像这样做:
ClassX *temp = [[ClassX alloc] init....];
UINavigationController *control = [[UINavigationController alloc] initWithRootViewController:temp];
然后显示控件视图 第一印象,我认为问题是由
UIButton*bookmarkButton=[UIButton buttonwhithtype:UIButtonTypeCustom]引起的代码>,但我不是很确定。
如果需要,可以这样修改。它确实有效
- (void)viewDidLoad
{
[super viewDidLoad];
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:@"My NavigationBar"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 0, 70, 30)];
[button setImage:[UIImage imageNamed:@"bookmark.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.rightBarButtonItem = buttonItem;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[buttonItem release];
[button release];
}
我想这个链接很有帮助:)