11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rubymotionで動的にtabBarを表示する方法

Last updated at Posted at 2014-03-09

Safariのブラウザのようにスクロールの状況に応じて、tabBarを隠したいと思い実装した際のメモ。

※初歩的なミスもありかなり手こずりました。。。
rubymotionで実装されたソースは無かったので、参考になれば m(_ _)m

とにかくまずはググってみるという事で、下記がヒット。

[Hide UITabBar of UITabBarController with animation]
(http://www.developers-life.com/hide-uitabbarcontrolleruitabbar-with-animation.html)

UITabBarControllerのTabBarを非表示にする

どうやら、問題になりそうなのは
self.tabBarController.tabBar.hidden = true と非表示にしたとしても、tabBarがあった場所に黒いブロックが残るらしい。

試しにやってみると、、、
tabbar_pic.png

画像のような感じで確かに黒い部分が残った。
なので上記を参考にしながら、rubymotion用に書いてみた。

※スクロールの上下の感知は下記を参考にしてください。
[[iOS SDK] UITableView で、上下のスクロール方向を感知する]
(http://rakuishi.com/archives/3596)

UITabBarControllerを継承したサブクラスに下記を実装。
(アニメーション使っていい感じに消えるようにしてます)

hoge_controller.rb
class HogeController < UITabBarController

  def showTabBar(isShow)
    UIView.animateWithDuration(0.2,
      animations: lambda { change_subview_size(isShow) },
      completion: lambda { |finished| self.tabBar.hidden = !isShow }                                                   
    )   
  end   
  #self.tabBar.hidden = !isShow は無くても問題なし
  #メソッドを呼び出すかどうかの判定でself.tabBar.hidden?を使っているため実装している

  def change_subview_size(isShow)
    appHeight = UIScreen.mainScreen.bounds.size.height
    self.view.subviews.each do |view|
      viewRect = view.frame
      resetVal = isShow ? (appHeight - 49) : appHeight
      if view == self.tabBar
        viewRect.origin.y = resetVal
      else
        viewRect.size.height = resetVal
      end
      view.setFrame(viewRect)
    end
  end 
end

これでいけるかなーと思ったが、相変わらず黒いブロックが消えない。。。
replで値をみたり、ログを確認してみるも原因がつかめない。。。

原因は、TableViewControllerを継承したもので実装していたのが原因でした。
[TableView のサイズを変更する]
(http://landau-post-that.blogspot.jp/2012/02/tableview.html)

TableViewのフレームサイズを変える際は、ViewControllerの上にtableviewをaddSubviewさせないと変更できないらしい。。。

なんと(´д`)(´д`)(´д`)

実際にUIViewControllerを継承させたものにtableviewをaddSubviewすると上手くいきました。

※やり方がが分からない方は下記が参考になるかと。
[UIViewにUITableViewを貼付ける]
(http://ameblo.jp/iichin0708/entry-11351848328.html)

何はともあれ、上手くいってよかった。

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?