如何制作更圆润的UITextField?就像iPad上Safari中的搜索字段一样

Hor*_*hiv 9 iphone uitextfield uikit uisearchbar

我正试图让UITextField看起来像iPad上的safari应用程序中提供的谷歌搜索字段.该字段的目的是相同的(搜索框).

我知道我可以使用UISearchBar,但我必须使用hackish代码来摆脱放大镜图标和背景,我不希望这样.

我正在使用Apple使用的TextField作为搜索框附加图像.如何修改UITextField以使其外观和行为类似于此屏幕截图中的搜索字段? 替代文字

我试图修改UITextField的图层roundedCorners属性,但这不能按预期工作.

很感谢任何形式的帮助!

谢谢!

sam*_*sam 17

您可以在任何UIView-Subclass上设置Corner-Radius.

  1. 将QuartzCore.framework添加到您的项目中
  2. 添加#import <QuartzCore/QuartzCore.h>您的UIViewController子类(您可以在其中访问您选择的UITextfield实例)
  3. 在viewDidLoad/viewWillAppear中(或者您的UITextfield已经实例化的任何其他地方调用 [yourTextField.layer setCornerRadius:14.0f];
  4. 使用cornerRadius值,直到它看起来很好


Vin*_*kar 10

使用以下代码.它适用于我:

searchField= [[UITextField alloc] initWithFrame:CGRectMake(0,0,150,31)];
searchField.delegate = self;
searchField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
searchField.textAlignment = UITextAlignmentLeft;
searchField.font = [UIFont fontWithName:@"Helvetica" size:15];
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
searchField.clearsOnBeginEditing = NO;
searchField.autocapitalizationType = UITextAutocapitalizationTypeNone;
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.keyboardType = UIKeyboardTypeURL;
searchField.returnKeyType = UIReturnKeySearch;        
searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchField.rightViewMode = UITextFieldViewModeUnlessEditing;
searchField.layer.cornerRadius = 17;
searchField.placeholder=@"Google Search";
searchField.borderStyle = UITextBorderStyleRoundedRect;//To change borders to rounded
searchField.layer.borderWidth = 1.0f; //To hide the square corners 
searchField.layer.borderColor = [[UIColor grayColor] CGColor]; //assigning the default border color
UIBarButtonItem *searchFieldItem = [[UIBarButtonItem alloc] initWithCustomView:searchField];
Run Code Online (Sandbox Code Playgroud)


Mic*_*han -4

使用具有所需圆角边缘的图形,然后将文本视图放置在顶部。