2012-05-30 138 views
0

我使用UISearchBar + Interface Builder中的UISearchBar + SearchDisplayController提供的UISearchBar,搜索欄位於視圖的右側,我需要在激活時向左擴展,但是系統似乎向右擴展(在我的情況下,它超越了屏幕),我試圖在啓動時對它進行動畫處理,但系統的動畫仍然存在,並且存在一些奇怪的動畫。UISearchBar:奇怪的擴展動畫

有什麼辦法解決這個問題嗎?

非常感謝!

回答

2

這裏是我的回答類似的問題發佈:herehere

到正確的動畫,關鍵是要指定UIViewAnimationOptionLayoutSubviews作爲動畫的選項。

這裏是我的全部答案,這些類似的問題:

通常你不會想要把一個搜索欄工具欄裏面,但是,似乎你想要做的相似,我做了一件。

因此,這裏是我是如何做到的,它可以被稱爲黑客,但它就像一個魅力:)

首先,你必須將它設置在Interface Builder是這樣的:

enter image description here

請注意,搜索不是工具欄的子項,而是位於上方。

搜索欄應該有「清晰的顏色」背景和靈活的左,右和寬度自動調整掩碼。

您在工具欄下面放置一個黑色背景的1像素標籤,即。 [x = 0,y = 44,width = 320或frame width,height = 1],也是靈活的左,右和寬度自動調整掩碼。這是爲了在搜索顯示控制器顯示錶格後隱藏您獲得的一個可見像素視圖。嘗試一下沒有理解我的意思。

您設置了任何工具欄項目,並確保有他們的網點,因爲您將需要這些。

,現在的代碼...

當你開始搜索

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller 
{ 
    // animate the search bar to the left ie. x=0 
    [UIView animateWithDuration:0.25f animations:^{ 
     CGRect frame = controller.searchBar.frame; 
     frame.origin.x = 0; 
     controller.searchBar.frame = frame; 
    }]; 
    // remove all toolbar items 
    [self.toolbar setItems:nil animated:YES]; 
} 

當您結束搜索

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller 
{ 
    // animate search bar back to its previous position and size 
    // in my case it was x=55, y=1 
    // and reduce its width by the amount moved, again 55px 
    [UIView animateWithDuration:0.25f 
          delay:0.0f 
         // the UIViewAnimationOptionLayoutSubviews is IMPORTANT, 
         // otherwise you get no animation 
         // but some kind of snap-back movement 
         options:UIViewAnimationOptionLayoutSubviews 
        animations:^{ 
         CGRect frame = self.toolbar.frame; 
         frame.origin.y = 1; 
         frame.origin.x = 55; 
         frame.size.width -= 55; 
         controller.searchBar.frame = frame; 
        } 
        completion:^(BOOL finished){ 
         // when finished, insert any tool bar items you had 
         [self.toolbar setItems:[NSArray arrayWithObject:self.currentLocationButton] animated:YES]; 
        }]; 
} 

有了這個,我得到了一個漂亮的動畫如下: )

enter image description here

enter image description here