10

iOS7で検索アイコンとプレースホルダーテキストが常に真ん中に表示されます。検索バーのテキストフィールドのテキスト配置を左に変更しようとしましたが、うまくいきませんでした。iOS7で検索バーの検索アイコンを左揃えで表示する方法はありますか

次の行を試しましたが、うまくいきませんでした:

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.textAlignment = NSTextAlignmentLeft;

[_searchArticle setImage:image forSearchBarIcon:UISearchBarIconSearch    state:UIControlStateNormal];
UIimageView *im = [UIImageView alloc]init];
im.image = image;
txfSearchField.leftView =im;
4

6 に答える 6

0

1 つの uitextField のサブクラス

initWithFrame の 2

  [self setBackgroundColor:[UIColor clearColor]];
        [self setTextAlignment:NSTextAlignmentLeft];
        [self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
        [self setAutocorrectionType:UITextAutocorrectionTypeNo];
        [self setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [self setPlaceholder:@"Search"];
        [self setLeftView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search-icon"]]];
        [self setLeftViewMode:UITextFieldViewModeAlways];

3 メイン クラスに UItextfieldDelegate を含め、次のデリゲート メソッドを実装します。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString* searchString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    searchYourFriendsArray = [[NSMutableArray alloc] init];

    if(searchString.length > 0 )
    {
        NSMutableArray *searchArray = [NSMutableArray arrayWithArray:yourFriendsArray];
        NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"FirstName beginswith[C] %@ OR LastName beginswith[C] %@",searchString,searchString];
        searchYourFriendsArray  = [NSMutableArray arrayWithArray:[searchArray filteredArrayUsingPredicate:predicate]];

       }

    return YES;
}
于 2013-10-10T15:36:40.063 に答える
0

iOS 9 の回避策:

extension ViewController: UISearchBarDelegate {

    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        if let searchTextField = searchBar.valueForKey("_searchField") as? UITextField {
            if let placeholderLabel = searchTextField.valueForKey("_placeholderLabel") as? UILabel {
                if searchTextField.textColor == placeholderLabel.textColor {
                    searchTextField.text = ""
                    searchTextField.textColor = UIColor.blackColor()
                    searchTextField.clearButtonMode = .Always
                }
            }
        }
        return true
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        if searchBar.text == "" {
            if let searchTextField = searchBar.valueForKey("_searchField") as? UITextField {
                if let placeholderLabel = searchTextField.valueForKey("_placeholderLabel") as? UILabel {
                    searchBar.text = "Placeholder"
                    searchTextField.textColor = placeholderLabel.textColor
                    searchTextField.clearButtonMode = .Never
                }
            }
        }
        searchBar.resignFirstResponder()
    }

}
于 2016-02-29T15:13:15.490 に答える
0

このメソッドを呼び出すと、スクリーンショットのようになります。 ここに画像の説明を入力

+(void)customizeDisplayForSeachbar:(UISearchBar *)searchBar
{ 
   searchBar.barTintColor = [UIColor whiteColor];
   searchBar.layer.borderWidth = 0.0;
   searchBar.layer.borderColor = [UIColor clearColor].CGColor;

    UITextField *txfSearchField = [searchBar valueForKey:@"_searchField"];
    UIImage *imageIcon=[UIImage imageNamed:@"search_icon.png"];
    float iconWidth=14;
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(15, (txfSearchField.superview.frame.size.height-iconWidth)/2-1, iconWidth, iconWidth)];
    imgView.contentMode=UIViewContentModeScaleAspectFit;
    imgView.image=imageIcon;
    txfSearchField.leftView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
    txfSearchField.leftViewMode=UITextFieldViewModeAlways;
    [txfSearchField.superview addSubview:imgView];
}
于 2016-10-25T04:36:42.863 に答える