简体   繁体   中英

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. 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.

The frame is a rectangle of type CGRect . 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 . 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.

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. Increasing the x value of your text field's frame will move the text field to the right.

You can do this eg by calling the performing the following operation after your UITextField is initialized (assumed its variable name is 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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