Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何更改多个UITextField边框样式和颜色_Objective C_Ios7_Uitextfield_Uistoryboard_Uitextborderstyle - Fatal编程技术网

Objective c 如何更改多个UITextField边框样式和颜色

Objective c 如何更改多个UITextField边框样式和颜色,objective-c,ios7,uitextfield,uistoryboard,uitextborderstyle,Objective C,Ios7,Uitextfield,Uistoryboard,Uitextborderstyle,编辑#2 从我得到的回答来看,我似乎在迷惑人们(以及后来的我自己)。让我们把这个问题简化一些- 我希望为给定的ViewController中的所有文本字段提供以下内容: textField.layer.cornerRadius=8.0f; textField.layer.masksToBounds=YES; [[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171

编辑#2

从我得到的回答来看,我似乎在迷惑人们(以及后来的我自己)。让我们把这个问题简化一些-

我希望为给定的
ViewController
中的所有文本字段提供以下内容:

textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
textField.layer.borderWidth= 1.0f;
我应该在何处执行此操作,以及如何执行此操作,以使其不会给我层属性的错误(即,当我尝试在
-(void)viewDidLoad
中或之后执行此操作时,我会在每一行中收到一个错误,说明在
ViewController
类型的对象上找不到“属性‘层’”

编辑#1 子类代码的完整部分,以帮助识别问题:

@interface InputTextField : UITextField
@end
@implementation InputTextField

- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 5;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 5;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;

InputTextField *textField=[[InputTextField alloc]init];
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
textField.layer.borderWidth= 1.0f;
}

@end
原创帖子

我在更改特定视图控制器中一系列文本字段的边框样式和颜色时遇到问题。我想调整视图中的一组文本字段。它们都已被赋予自定义类“InputExtField”。但是,此线程中提出的解决方案:不解决我的问题

以下是我想要实现的风格和颜色:

textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
textField.layer.borderWidth= 1.0f;
我还导入了
QuartzCore/QuartzCore.h
。我想设置它,这样我的应用程序中带有自定义类
inputExtField
的每个文本字段都会以这个背景显示。此时,每当我在应用程序中运行这个时,这些字段都会采用我在故事板中设置的背景值(目前没有边框)

感谢您的帮助!

问题出在这里:

- (CGRect)editingRectForBounds:(CGRect)bounds {
之后

未调用任何内容。请将代码移到返回上方

编辑#1

将此添加到子类:

- (void)layoutSubviews {
    CALayer *layer = self.layer;
    layer.cornerRadius = 8;
    layer.borderWidth = 1;
    layer.borderColor = [UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0].CGColor;
    [super layoutSubviews];
}

您正在一个方法中实例化一个
inputExtField
,该方法只要求您使用
CGRect
来绘制一个矩形,这已经没有多大意义,但无论如何都不会调用该代码,因为它位于
return
语句之后

如果希望所有的
InputTextField
以特定的方式查看,请在实例化
InputTextField
时设置

使用类似以下内容的情节提要:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.layer.cornerRadius=8.0f;
        self.layer.masksToBounds=YES;
        [self.layer setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
        self.layer.borderWidth= 1.0f;
    }
    return self;
}

您是否已将此代码放入子类中?您在子类中的何处调用此代码?如果粘贴完整的子类代码,则更容易理解。@Jkmn-是的,我已将代码添加到子类中(在我的.m文件的接口设置中)。检查问题中的完整代码选择。@Stas Zhukovskiy-我已经添加了上面的完整代码部分。感谢您的帮助。遗憾的是,这并没有解决问题。在编辑RectForBounds部分之间移动它根本不会影响字段。在顶部的RectForBounds部分之间移动它会导致返回错误。我还试图删除以下行:InputExtField*textField=[[InputExtField alloc]init];希望它能工作,因为我们已经在使用InputExtField,但是它会导致每行错误。将textField定义为InputExtField会导致错误在“UITextField”类型的对象上找不到属性“layer”。为了澄清-我不再使用子类定义UITextFields的其他属性。因此,您的-(void)不幸的是,layoutSubviews在调用时不起作用。Hey@Jkmn-这不起作用,因为在VC上找不到layer属性。此代码用于子类化UITextField的类(您编写的类
@interface InputTextField:UITextField
)。
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.layer.cornerRadius=8.0f;
        self.layer.masksToBounds=YES;
        [self.layer setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
        self.layer.borderWidth= 1.0f;
    }
    return self;
}