简体   繁体   English

更改UITextField的宽度?

[英]Change UITextField width?

I'm currently in the process of implementing some sort of browser in my app. 我目前正在我的应用程序中实现某种浏览器。 However, I'm fairly new to iOS programming and therefore don't know how to change the width of a textfield programmatically. 但是,我是iOS编程的新手,因此不知道如何以编程方式更改文本字段的宽度。 What happens in the current implementation is that the textfield goes over the back button of the navigation controller. 在当前的实现中,发生的事情是文本字段超过了导航控制器的后退按钮。 Thus, I need the textfield to leave some room on the left. 因此,我需要文本字段在左侧留一些空间。

Does anyone know how to do this? 有谁知道如何做到这一点? Is this even possible? 这有可能吗?

/* Create the page title label */
UINavigationBar *navBar = self.navigationController.navigationBar;
CGRect labelFrame = CGRectMake(kMargin, kSpacer,
                               navBar.bounds.size.width - 2*kMargin, kLabelHeight);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:12];
label.textAlignment = NSTextAlignmentCenter;
[navBar addSubview:label];
self.pageTitle = label;

/* Create the address bar */
CGRect addressFrame = CGRectMake(kMargin, kSpacer*2.0 + kLabelHeight,
                                 labelFrame.size.width, kAddressHeight);
UITextField *address = [[UITextField alloc] initWithFrame:addressFrame];
address.autoresizingMask = UIViewAutoresizingFlexibleWidth;
address.borderStyle = UITextBorderStyleRoundedRect;
address.font = [UIFont systemFontOfSize:17];
address.keyboardType = UIKeyboardTypeURL;
address.autocapitalizationType =UITextAutocapitalizationTypeNone;
address.autocorrectionType = UITextAutocorrectionTypeNo;
address.clearButtonMode = UITextFieldViewModeWhileEditing;
[address addTarget:self
            action:@selector(loadRequestFromAddressField:)
  forControlEvents:UIControlEventEditingDidEndOnExit];
[navBar addSubview:address];
self.addressField = address;

You can control the positioning and the size of a UI element, in this case your UITextField by modifying its frame property. 您可以通过修改UI元素的frame属性来控制UI元素的位置大小 (在本例中为UITextField

The frame is a rectangle of type CGRect . frameCGRect类型的矩形。 It consists of four components that specify its positioning and size within the coordinate space of the screen. 它由四个组件组成,这些组件指定其在屏幕坐标空间内的位置和大小。

The first two components are: x and y . 前两个成分是: xy These represents the position of the rectangle. 这些代表矩形的位置。 The top left of the screen is the point (0,0), so your custom x and y values are relative to this point. 屏幕的左上方是点(0,0),因此您的自定义xy值是相对于该点的。

In your case, you need to modify the x property of the view, as you described that your UITextField is too far on the left. 在您的情况下,您需要修改视图的x属性,因为您描述了UITextField在左侧太远。 Increasing the x value of your text field's frame will move the text field to the right. 增加文本字段框架的x值会将文本字段向右移动。

You can do this eg by calling the performing the following operation after your UITextField is initialized (assumed its variable name is address ): 您可以执行此操作,例如,在初始化UITextField之后(假设其变量名称为address ),调用执行以下操作:

address.frame = CGRectMake(address.frame.origin.x + 10.0, address.frame.origin.y, address.frame.size.width, address.frame.size.width)

This will shift your text field 10 points to the right. 这会将您的文本字段向右移动10点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM