0
イメージとテキストの両方を含むカスタムUIBarButtonItemを作成し、UIButtonタップ領域をカスタマイズ(縮小)したいと考えています。これをプログラムで実現するには、次のコードスニペットを使用しています。しかし、UIViewにサブビューとして追加するとUIButtonが消えます。誰かが間違っていることを教えてもらえますか? バーボタンのタップ領域を減らすために、カスタムUIViewにカスタムUIButtonを埋め込みます。customViewを使用しているときUINavigationBarButtonが機能しない
//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = @"";
UIView *customBackView = [[UIView alloc] initWithFrame:CGRectMake(220,0,50,40)];
customBackView.backgroundColor = [UIColor clearColor];
//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeSystem];
tempButton.frame=CGRectMake(240,2,40,40);
[tempButton setImage:[UIImage imageNamed:@"offline_bk.png"]
forState:UIControlStateNormal];
//Add selector to info button
[tempButton addTarget:self action:@selector(onTapDone:) forControlEvents:UIControlEventTouchUpInside];
[customBackView addSubview:tempButton];
[customBackView bringSubviewToFront:tempButton];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:customBackView];
//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;
//Add NavigationBar on main view
navBar.items = @[navTitle];
[self.view addSubview:navBar];
おかげで、しかし、UIButtonの画像が表示されていないボタンは右コーナーに存在している場合、我々は識別することはできません。 – Gurp321
することができます。ビューのデバッガを使用してボタンの存在についてビュー階層をトラバースすることができます。画像のサイズに問題があるかもしれません。ボタンのサイズに従って画像ファイルを試してください。 –
ダミー画像で試してみました。使用している解像度が正しく、プロジェクトのバンドルに正しく追加されました –