问题
What I am trying to do: I want to move the x and y coordinates of my search bar on a touch event. I have placed my code in viewDidLoad to test it out. However I am getting an error.
Error: Property 'frame' not found on object of type 'UISearchBar'.
My code:
NSNumber *newX = @0;
NSNumber *newY = @0;
[UIView animateWithDuration:0.3f animations:^{
[UISearchBar setFrame:CGRectMake(newX, newY, UISearchBar.frame.size.width,
yourSearchBar.frame.size.height)];
}
completion:^(BOOL finished) {}
];
Ultimately I would like that animation/move to be executed when the search bar is selected and then move back after deselect.
回答1:
UISearchBar
is a class. It doesn't have a class method setFrame
. It does have an instance method though and that's what you should be using. So, you need an instance of UISearchBar
, either that you already have or you need to create one.
If this is being done when the search bar is selected, your code would be in the delegate method searchBarTextDidBeginEditing:
and you would use the supplied parameter:
[searchBar setFrame:CGRectMake(newX, newY, searchBar.frame.size.width, yourSearchBar.frame.size.height)];
来源:https://stackoverflow.com/questions/17846660/property-frame-not-found-on-object-of-type-uisearchbar-error