0

如何调整 UITextField 的大小以适应整个 UINavigationBar?到目前为止,我有这个:

    locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];
    locationField.delegate = self;
    locationField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    locationField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
    locationField.textAlignment = UITextAlignmentLeft;
    locationField.borderStyle = UITextBorderStyleRoundedRect;
    locationField.font = [UIFont fontWithName:@"Helvetica" size:15];
    locationField.autocorrectionType = UITextAutocorrectionTypeNo;
    locationField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

我基本上想转向locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];适合 UINavigationBar,可能正在使用sizeToFit,只是不是 100% 确定如何实现它。

谢谢。

4

2 回答 2

2

你可以试试

[self.navigationItem setTitleView:locationField];

请参见下面的代码:

    UITextField *locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];
    locationField.delegate = self;
    locationField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    locationField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
    locationField.textAlignment = UITextAlignmentLeft;
    locationField.borderStyle = UITextBorderStyleRoundedRect;
    locationField.font = [UIFont fontWithName:@"Helvetica" size:15];
    locationField.autocorrectionType = UITextAutocorrectionTypeNo;
    locationField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.navigationItem setTitleView:locationField];
    [locationField release];
于 2012-06-16T21:31:53.903 回答
0

没关系,通过从以下位置切换 CGRect 来修复它:

UITextField *locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];

UITextField *locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,300,31)];
于 2012-06-17T17:27:16.497 回答