6

在我的應用程序中,在隱藏/顯示導航欄上查看框架更改?

我有導航控制器與根視圖控制器。

顯示/隱藏工作正常的導航欄。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    BOOL navbarhide=[self.navigationController.navigationBar isHidden]; 
    [self.navigationController setNavigationBarHidden:!navbarhide animated:YES]; 


} 

工作良好,但

當導航欄被隱藏然後查看幀變化。

當導航欄未隱藏時,則查看框架更改。

When navigation bar is not hidden ..see the press button and view's origin is below the bar, I dont want like this I want it to stick at navigation bar's origin[at the top of the page]

It is fine view is at its original position when bar is hidden. I want view to be stick at this position and turn bar hide/show without changing the view's frame.

在此先感謝...

編輯設置中設置self.view.frame不作任何效果。

回答

10

我有同樣的問題。在我的項目中,這是因爲視圖是滾動視圖。如果你的視圖是滾動視圖或表格視圖,你可以試試這個:

我將下面的代碼添加到控制器。

self.automaticallyAdjustsScrollViewInsets = NO; 

希望它能幫助你。

0

我認爲這是由不能被你改變默認的功能,但是通過使用以下代碼

myButton.frame = CGRectMake(X,Y,高度,寬度)來改變按鈕的位置;

隱藏導航欄後。

+0

其實這是測試代碼我要實現它向我的應用程序......如果你能得到.. –

+0

當你點擊按鈕,然後更改myButton的值。 更改值並嘗試此操作。 我想你會得到你的解決方案。 –

+0

是的,我可以移動按鈕,但在我的真實應用程序中,我有很多組件,所以移動組件很麻煩!!! 1 –

0

我認爲這可以幫助你

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
BOOL navbarhide=[self.navigationController.navigationBar isHidden]; 
[self.navigationController setNavigationBarHidden:!navbarhide animated:YES]; 
if(navbarhide) 
self.view.frame=CGRectMake(0,0, 320, 480); 
else 
    self.view.frame=CGRectMake(0,44, 320, 480); 
} 
+0

沒有影響...相同的行爲... –

+0

這不起作用,因爲在設置框架後導航開始隱藏。並刪除你所做的。 – hasan83

0

變化,當你在顯示和隱藏導航欄

3

你不能改變的self.view框架的self.view.frame。我不使用setNavigationBarHidden:來隱藏導航欄,而是直接更改self.navigationController.navigationBar的框架。這樣,self.view.frame不會改變。

CGRect frame = (navBarhidden) ? CGRectMake(0, -24,self.view.bounds.size.width,44) :  CGRectMake(0, 20,self.view.bounds.size.width,44); 
self.navigationController.navigationBar.frame = frame; 
+0

但是在舊的地方留下了黑色的空間。 – hasan83

+0

我試過這個,但它給了我太多的錯誤 – Jasper

2

我在全屏顯示UIView時遇到了這個問題。調整框架爲我工作。 (SWIFT)

隱藏的導航欄:

self.navigationController!.navigationBar.hidden = true 
    self.view.frame.origin.y -= 64 
    self.view.frame.size.height += 64.0 

再次顯示的導航欄:

self.navigationController!.navigationBar.hidden = false 
    self.view.frame.origin.y += 64 
    self.view.frame.size.height -= 64.0