SizeToFit导航栏项(UITextField)

时间:2012-06-16 18:19:23

标签: iphone uitextfield uinavigationbar

如何调整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%确定如何实现它。

感谢。

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

没关系,通过切换CGRect来修复它:

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

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