如何在UISearchbar处于活动/非活动状态时放大和缩小?

时间:2017-05-27 09:02:06

标签: ios objective-c uisearchbar

我的代码如下: -

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    self.navigationController.navigationBar.hidden = TRUE;
    _r = self.view.frame;
    _r.origin.y = -44;
    _r.size.height += 44;
    self.view.frame = _r;

    [searchBar setShowsCancelButton:YES animated:YES];

    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{

    NSLog(@"cancel button pressed");
    //This'll Hide The cancelButton with Animation
    [searchBar setShowsCancelButton:NO animated:YES];
    _r.origin.y = 44;
    _r.size.height -= 44;
    self.view.frame = _r;
    [searchBar resignFirstResponder];
    //remaining Code'll go here
}

上面的代码没有按预期工作。它破坏了视图而不是放大和缩小。当按下“取消”按钮时,它变为下图。如何在iPhone的“设置”中拥有完美的放大和缩小效果,例如Apple的搜索栏。 Spoiled view

1 个答案:

答案 0 :(得分:0)

您需要使用UISearchBar对象中的CGAffineTransform

CGFloat zoomIn = 1, zoomOut = .1;

CGAffineTransform tfIn = CGAffineTransformMakeScale(zoomIn, zoomIn);
CGAffineTransform tfOut = CGAffineTransformMakeScale(zoomOut, zoomOut);

[UIView animateWithDuration: .2 delay: 0 options: 0 animations: ^{
    self.transform = tfIn;

} completion: ^(BOOL finished) {
    // Nothing
}];

// Action from button to zoom out
self.transform = tfOut;