IOS 15设置UISearchBar 未编辑居中,编辑居左

2022-01-06  本文已影响0人  九月oc

原理很简单, 不编辑的时候设置偏移量居中,编辑的时候置0




 [searchBar.searchTextField addTarget:self action:@selector(BeginEdit:) forControlEvents:UIControlEventEditingDidBegin];

        [searchBar.searchTextField addTarget:self action:@selector(EndEdit:) forControlEvents:UIControlEventEditingDidEnd];


-(void)BeginEdit:(UITextField*)textfiled{

    [UIView animateWithDuration:1 animations:^{

        [self->_searchBar setPositionAdjustment:UIOffsetMake(0, 0) forSearchBarIcon:UISearchBarIconSearch];

    }];

}



-(void)EndEdit:(UITextField*)textfiled{

    [UIView animateWithDuration:1 animations:^{

        [self->_searchBar setPositionAdjustment:UIOffsetMake(ComponentSearchOffset, 0) forSearchBarIcon:UISearchBarIconSearch];

    }];

}


上一篇下一篇

猜你喜欢

热点阅读