繁体   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