当导航回到父视图控制器 - iOS时,UIButton会在Tab Bar后面

时间:2016-01-22 13:34:08

标签: ios objective-c uibutton uitabbarcontroller uitabbar

我有2 UIButton个放在TabBarViewController上,(也有嵌入式导航控制器)。

在最后一个ViewController中(在导航控制器堆栈中),我必须隐藏标签栏,我已经成功实现了。

问题是,当我导航回parentViewController时,UIButton位于标签栏后面(请参阅下面的GIF图片):

红色记录按钮和红色“保留”按钮是UIButton,而不是tabBarButtons

GIF

这就是我在UIButtons中添加tabBarViewController的方式:

self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[self.button1 addTarget:self action:@selector(recordButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.button1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
self.button1.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[self.button1 setBackgroundImage:[UIImage imageNamed:@"bar_button_record"] forState:UIControlStateNormal];


heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
    self.button1.center = self.tabBar.center;
else
{
    CGPoint center = self.tabBar.center;
    center.y = center.y - heightDifference/2.0 - 0;
    self.button1.center = center;
}

[self.view addSubview:self.button1];

另外红色的“保留”按钮也添加了与上面类似的代码。

现在在我的上一篇navigationViewController中,我通过以下代码隐藏了tabBar

#pragma mark - Overriden UIViewController methods, this function gets called automatically when viewController is loaded
- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

上面的函数返回YES隐藏了tabBar,但它是那个使按钮在前往View控制器的情况下落后于tabBar的函数。 如果我将其更改为NO,则按钮不会落后于tabBar,但tabBar也不会隐藏在最后viewController上。

请建议一种方法,以不同方式隐藏tabBar,或将UIButton放在前面。

谢谢!

4 个答案:

答案 0 :(得分:2)

好的,找到了解决方案!而不是隐藏tabBar

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

tabBar上隐藏[tabBarController.tabBar setHidden:YES] viewController。然后,当导航回来时,请[tabBarController.tabBar setHidden:NO];中的viewWillDisappear

这样subviews中的tabBarController排列保持不变:)

答案 1 :(得分:0)

您是否尝试在视图布局时将按钮置于前面?

例如

self.view.bringSubviewToFront(self.button1)

或者在tabBar

上面实例化它
self.view.insertSubview(self.button1, aboveSubview: self.tabBar)

答案 2 :(得分:0)

尝试了@Sha的回答,另外这就是我做的操作成功 确保在 Last ViewController

中添加此代码
var tabbarViewController: tabbarViewController?
var tabbarHoldonFrame = CGRect()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.hideTabbar()
    }
    func hideTabbar() {
        // get instance of tabbar controller
        tabbarViewController = self.navigationController?.tabBarController as? tabbarViewController
        // hide tabbar
        tabbarViewController?.tabBar.isHidden = true
        // hide your additional button placed above tabbar
        tabbarViewController?.buttonCreateContest.isHidden = true
        // hold it's frame in tabbarHoldonFrame, set zero temporarily 
        tabbarHoldonFrame = (tabbarViewController?.tabBar.frame)!
        tabbarViewController?.tabBar.frame = CGRect.zero
    }
    override func viewWillDisappear(_ animated: Bool) {
        self.showTabbar()
    }
    func showTabbar() {
        // show it
        tabbarViewController?.tabBar.isHidden = false
        // show your button
        tabbarViewController?.buttonCreateContest.isHidden = false
        // add default frame
        tabbarViewController?.tabBar.frame = tabbarHoldonFrame
    }

答案 3 :(得分:0)

在自定义标签栏控制器中添加以下方法,以修复自定义按钮问题,并将其置于标签栏后面。

Objective-C代码:

-(void)viewDidLayoutSubviews {
    if (self.centerButton != nil) {
        [self.view bringSubviewToFront:self.centerButton];
        for (UIView *vw in self.view.subviews) {
            if ([vw isKindOfClass:[UITabBar class]]){
                if(vw.hidden){
                    self.centerButton.hidden = true;
                }
                else {
                    self.centerButton.hidden = false;
                }
            }
        }
    }
}

Swift Code:

override func viewDidLayoutSubviews() {
    if self.centerButton != nil {
        self.view.bringSubview(toFront: self.centerButton)

        for vw in self.view.subviews {
            if let subView = vw as? UITabBar {
                if subView.isHidden == true {
                    self.centerButton.isHidden = true
                } else {
                    self.centerButton.isHidden = false
                }
            }
        }
    }
}

它只是有点慢,当视图从隐藏变为显示或显示隐藏时,会有大约0.5秒的延迟。