簡體   English   中英

導航欄中的iOS自定義搜索欄帶有動畫嗎?

[英]iOS custom search bar in navigation bar with animation?

我知道關於在導航欄中添加搜索欄的問題在這里被問過很多次了(我知道這個過程)。 但是我的問題是我必須顯示具有自定義背景的搜索欄,並且當我單擊添加到導航欄右側的按鈕時,它必須與動畫一起顯示/隱藏。 在當前搜索欄之前

在此處輸入圖片說明

最終的用戶界面想要(點擊搜索圖標后)-

像這個png

任何幫助,將不勝感激。

代碼是

//search bar
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width-10-60, 44)];
   // searchBar.translucent = NO;
    //searchBar.barTintColor = [UIColor clearColor];
    searchBar.backgroundColor = [UIColor clearColor];
    [self.navigationController.navigationBar   addSubview:searchBar];

    float delta = searchBar.frame.size.width;
    searchBar.frame = CGRectOffset(searchBar.frame, -delta, 0.0);
    searchBar.hidden = YES;
-(void)rightbuttonPressed
{
    // get the width of the search bar
    float delta = searchBar.frame.size.width;
    // check if toolbar was visible or hidden before the animation
    BOOL isHidden = [searchBar isHidden];

    // if search bar was visible set delta to negative value
    if (!isHidden) {
        delta *= -1;
    } else {
        // if search bar was hidden then make it visible
        searchBar.hidden = NO;
    }

    // run animation 0.7 second and no delay
    [UIView animateWithDuration:0.7 delay: 0.0 options: UIViewAnimationOptionCurveEaseIn animations:^{
        // move search bar delta units left or right
        searchBar.frame = CGRectOffset(searchBar.frame, delta, 0.0);
    } completion:^(BOOL finished) {
        //if the bar was visible then hide it
        if (!isHidden) {
            searchBar.hidden = YES;
        }
    }];
}

不要將搜索欄添加為導航欄上的子視圖。 而是將其設置為當前視圖控制器的navigationItem的titleView。

在UIView中添加搜索欄,然后將視圖分配給titleView。 像平時一樣,在此視圖內對搜索欄進行動畫處理。 對於背景, UISearchBar具有backgroundImage屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM