开发者

How to prevent UIView from sliding down a little when navigationBarHidden is set to YES

开发者 https://www.devze.com 2023-01-29 18:11 出处:网络
Basically I pushed a full screen UIViewController into a UINavigationController and would like to toggle whether the navigationBarHidden is seem.

Basically I pushed a full screen UIViewController into a UINavigationController and would like to toggle whether the navigationBarHidden is seem.

Howerever I find that after I set navigationBarHidden to YES, the whole UIView will slide down a 44 pixels to make the room for navigationBar. How to I disable this behavior? Since I al开发者_StackOverflowready set barStyle to UIBarStyleBlackTranslucent and the sliding off is really not necessary.

//init viewController
[self.navigationController pushViewController:viewController animated:YES];

Edit:

I am also dealing with pushing a scrollview to the navigation controller.. I find that after setting the navigation bar to be visible. My scroll view will be pushed down a little bit, when I scroll the view manually in that state, the scrollView itself will slide up again. My question is how to prevent the scrollView from sliding down.

Frame size is the same before the view slide down or up.


Try resetting the view frame. Do something like this after you set the bar to hidden:

viewController.frame  = self.navigationController.frame;

You can always break it there and see what the values are and could even build a custom frame accordingly to make it fit. I have had to do that before...


I solved this problem by adding the scrollView to a UIView and set the UIView as UIViewController's view.


I think this is a bug/feature of UINavigationController and I'm not aware of any way to disable it. It sucks. I've always had to readjust my view frames manually. This is way more of a pain than it should be when dealing with rotation, scroll views....


Might have something to do with contentInset. When setting navigationBarHidden property, if root view of the current view controller is a scroll view, its contentInsets.top is set to the height of the top bars.

To prevent that, immediately after the line that changes navigationBarHidden, reset self.scrollView.contentInset to whatever original value you have.


Add the following in your viewDidLoad method:

self.wantsFullScreenLayout = YES;

It tells the view controller to already place subviews under the navigation bar.


None of the answers here worked for me. Instead, you can reset the inset on the UIScrollView during viewDidLayoutSubviews() as seen below (in Swift):

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // prevent the scrollview from adding some inset for the status bar
    self.detailScroll.contentInset = UIEdgeInsetsZero
}


I has tried, which works only is that move navigationBar with its origin y, and change navigationBar's subViews's alpha to 0.f; if want it show back, just do it reverse


self.navigationController.navigationBar.alpha = 0.0;

this can be a easy solution

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号