UISearchBar search icon isnot left aligned in iOS7

前端 未结 6 837
我在风中等你
我在风中等你 2020-12-06 02:35

In iOS7 the search icon and place holder text are always appearing in middle.I tried changing the text alignment of the text field in search bar to left but it didn\'t work.

6条回答
  •  暖寄归人
    2020-12-06 02:41

    1 subclass a uitextField

    2 in initWithFrame

      [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 in main class include UItextfieldDelegate and implement following delegate method

    - (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;
    }
    

提交回复
热议问题