Animating the width of UISearchBar frame

限于喜欢 提交于 2019-12-03 07:28:41

问题


Is it possible to animate the frame width of a UISearchBar? I find when I apply uiview animations to widen the bounds of a search bar it pops immediately to the final result as if the object internally is assuming control of how it animates and not allowing me to apply my own animations to it smoothly.

If I animate the position it moves smoothly, but I suspect the fact that the text input adjusts according to the presence of the cancel button might mean we don't have public access to animate the width through UIView animation. The sample snippet below slides the bar from x = 0 to 100 but pops the width to 600 pixels wide.

CGRect searchBarFrame = self.searchViewController.searchBar.frame;
searchBarFrame.origin.x = 100;
searchBarFrame.size.width = 600;
[UIView animateWithDuration:1.0 
                      delay:0.0 
                    options:0 
                 animations:^{
                     self.searchViewController.searchBar.frame = searchBarFrame;
                 }
                 completion:^(BOOL completion){
                 }];

回答1:


there is an "issue" with UISearchBar due to the inner views forcing the resize to ignore the animation. However, this can be overcome by the use of - layoutSubviews. I have included the expand and contract code in my project below

[UIView animateWithDuration:.3
                 animations:^ {
                     CGRect newBounds = locationSearch.frame;
                     newBounds.size.width += 215; //newBounds.size.width -= 215; to contract
                     locationSearch.frame = newBounds;
                     [locationSearch layoutSubviews];
                 }];

Hope this helps.




回答2:


FYI, you can use UIViewAnimationOption instead of calling layoutsubviews explicitly, So the code would look something like this..

[UIView animateWithDuration:0.5
                              delay:0
                            options:UIViewAnimationOptionLayoutSubviews
                         animations:^{
                             //Set the frame you want to the search bar
                         }
                         completion:^(BOOL finished) {

                         }];



回答3:


This is how to overcome enlargement just to the Left side in swift

(This code will enlarge/shrinks the searchBar 93 pixels over the left side when user start/end editing)


  1. SharedNavigationbBar is the UIView that implements the UISearchBarDelegate
  2. searchBarWidth is an outlet to a constrain holding the width of the UISearchBar

  1. An autolayout constrain must exists on your storyboard or nib file to allow resizing into the left side. In this case, the neighbord left component is an UIButton.


  1. Add the following code as an extension or inside your class to perform the animated resizing.

extension SharedNavigationBar: UISearchBarDelegate
{
//amount of pixels to enlarge to the left
 private var offsetSearchBarLeft:CGFloat
    {
    get {
        return 93
    }
 }

///Enlarges search bar
 func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

   self.animateSearchBar(self.searchBar, enlarge: true)
 }

///Shrinks search bar
 func searchBarTextDidEndEditing(searchBar: UISearchBar) {

   self.animateSearchBar(self.searchBar, enlarge: false)
 }

//shrinks or enlarge the searchbar (this will be the function to call inside the animation)
 private func animateSearchBar(searchBar:UISearchBar, enlarge:Bool)
 {
     ///Important here, for this to work, the option and the searchbar size must be handled this way
    UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.LayoutSubviews, animations: { [weak self] () -> Void in

    let multiplier: CGFloat = enlarge ? 1 : -1

    let origin = searchBar.frame.origin.x + self!.offsetSearchBarLeft * multiplier
    let width = searchBar.frame.width + self!.offsetSearchBarLeft * multiplier

    //This Block of code, setting the new frame, needs to be inside the animation in order to work
    var newBounds:CGRect  = searchBar.frame;
    newBounds.origin.x = origin
    newBounds.size.width = width

    //Sets the new frame
    self?.searchBarWidth.constant = width
    searchBar.frame = newBounds

   }, completion: nil)
 }

}


来源:https://stackoverflow.com/questions/9571995/animating-the-width-of-uisearchbar-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!