-1

I put two functions in the code but it gives me this error:

Overlapping accesses to 'frame', but modification requires exclusive access; consider copying to a local variable

how can i solve?

import UIKit

extension UITabBarController {
    
    func hideTabBar() {
        var frame = self.tabBarController?.tabBar.frame
        frame?.origin.y = self.view.frame.size.height + (frame?.size.height)!
        UIView.animate(withDuration: 0.5, animations: {
            self.tabBarController?.tabBar.frame = frame!
        })
    }

    func showTabBar() {
        var frame = self.tabBarController?.tabBar.frame
        frame?.origin.y = self.view.frame.size.height - (frame?.size.height)!
        UIView.animate(withDuration: 0.5, animations: {
            self.tabBarController?.tabBar.frame = frame!
        })
    }
    
}
7
  • add var frame: CGRect?, outside the function. Commented Apr 18, 2023 at 7:35
  • @OneCommentMan added, but now it gives me this error: Extensions must not contain stored properties Commented Apr 18, 2023 at 7:41
  • So the issue of your function is you accessing frame while you're trying to modify it. You can update your function to something like this. func hideTabBar() { guard var frame = self.tabBarController?.tabBar.frame else { return } let originY = self.view.frame.size.height + t.size.height frame.origin.y = originY UIView.animate(withDuration: 0.5) { self.tabBarController?.tabBar.frame = frame } } Commented Apr 18, 2023 at 7:44
  • t.size.height to frame.size.height Commented Apr 18, 2023 at 8:16
  • @OneCommentMan if i put it inside the extension of UITabBarController it doesn't work Commented Apr 18, 2023 at 8:21

1 Answer 1

0

The code seems to be violating Swift’s exclusive access rules but I’m not smart enough to know why! But rewriting the methods to avoid all the optionals fixes the compiler errors:

extension UITabBarController {
    func hideTabBar() {
        var frame = tabBar.frame
        frame.origin.y = view.frame.size.height + frame.size.height
        UIView.animate(withDuration: 0.5, animations: {
            self.tabBar.frame = frame
        })
    }

    func showTabBar() {
        var frame = tabBar.frame
        frame.origin.y = view.frame.size.height - frame.size.height
        UIView.animate(withDuration: 0.5, animations: {
            self.tabBar.frame = frame
        })
    }
}

I don’t think adjusting the tab bar’s frame is very safe and you might have problems (anything that triggers a relayout of the tab bar controller could make the tab bar appear again). If you are hiding the tab bar when pushing a view controller on a navigation stack then this might be better: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621863-hidesbottombarwhenpushed

Sign up to request clarification or add additional context in comments.

3 Comments

if i put it inside the extension of UITabBarController it doesn't work
maybe I have to remove self?
Your original question didn’t say the code was in an extension of UITabBarController. I have updated my answer but am using a Playground on iPad so only compiled the code. I have not run it to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.