Tito のコード スニペットを使用して、タブ バーにカスタム ボタンを追加しています: https://github.com/tciuro/CustomTabBar
(UITabbarController をサブクラス化し、カスタム ボタンを追加する
// .. created a UIButton *button
[self.view addSubview:button];
)
これは、「プッシュ時にボトムバーを非表示にする」オプションが有効になっているナビゲーションコントローラー内のサブビューの場合を除いて、ストーリーボードベースのアプリでうまく機能します。これにより、約束どおりタブ バーが非表示になりますが、カスタム ボタンは非表示になりません。タブバー自体にサブビューとしてボタンを追加する必要があるようですか? ボタンを表示さえしなかったこの醜いコードを試しました:
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view addSubview:button];
break;
}
}
何か案は?
更新: 私の解決策: 私の ApplicationDelegate では、次のメソッドを定義します。これらのメソッドは、viewWillAppear または viewWillDisappear メソッドで必要なときにいつでも呼び出します。
-(void)hideCenterButton:(BOOL)animated
{
if(animated){
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = -100;
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}
}
-(void)showCenterButton:(BOOL)animated
{
if(animated){
[UIView animateWithDuration:0.35
delay:0.0f
options:UIViewAnimationCurveLinear
animations:^{
CGRect frame = self.centerButton.frame;
frame.origin.x = (self.view.superview.frame.size.width / 2) - (self.centerButton.frame.size.width / 2);
self.centerButton.frame = frame;
}
completion:^(BOOL finished){
}];
}
}
タブバーと調和した滑らかな効果を得るために、アニメーションの持続時間を 0.35 秒に設定する必要がありました。