UISearchBar stretches text when it begins editing

我只是一个虾纸丫 提交于 2019-12-03 14:05:19

Your updated solution works well, except it stops the "Search" placeholder from being animated to the left when the Cancel button appears while there is no text. Checking searchBar.text restores the animation:

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
    // This avoids the text being stretched by the UISearchBar.
    if searchBar.text?.isEmpty == false {
        searchBar.setShowsCancelButton(true, animated: true)
    }
    return true
}

I suspect this may only be an issue for the Minimal UISearchBarStyle.

- (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);

Just call the method without animated.

if (!self.isShowCancelBtn) {
    [self.searchBar setShowsCancelButton:YES animated:NO];
    self.isShowCancelBtn = YES;
}else{
    [self.searchBar setShowsCancelButton:NO animated:NO];
    self.isShowCancelBtn = NO;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!