How to add a 1 pixel gray border around a UISearchBar TextField

后端 未结 6 2413
攒了一身酷
攒了一身酷 2021-02-19 14:21

I\'m trying to figure out how to add a 1 pixel stroke gray border to my UISearchBar in my app. The Facebook Messenger app accomplishes this quite well. (see pic below).

6条回答
  •  孤街浪徒
    2021-02-19 14:51

    Try this code:

    //First add the following import and macro:
    #import 
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    
    //Then change yourSearchBar border: 
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        for (id object in [[[yourSearchBar subviews] objectAtIndex:0] subviews])
        {
            if ([object isKindOfClass:[UITextField class]])
            {
                UITextField *textFieldObject = (UITextField *)object;
                textFieldObject.layer.borderColor = [[UIColor grayColor] CGColor];
                textFieldObject.layer.borderWidth = 1.0;
                break;
            }
        }
    }
    else
    {
        for (id object in [yourSearchBar subviews])
        {
            if ([object isKindOfClass:[UITextField class]])
            {
                UITextField *textFieldObject = (UITextField *)object;
                textFieldObject.layer.borderColor = [[UIColor grayColor] CGColor];
                textFieldObject.layer.borderWidth = 1.0;
                break;
            }
        }
    }
    

提交回复
热议问题