2012-06-16 106 views
0

如何调整UITextField以适合整个UINavigationBar?到目前为止,我有这样的:SizeToFit导航栏项目(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; 

我基本上要翻locationField = [[UITextField alloc] initWithFrame:CGRectMake(37,7,246,31)];以适应UINavigationBar的,可能使用sizeToFit,只要不是100%确定如何实现它。

谢谢。

回答

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]; 
+0

不幸的是,仍然看起来像这样:[链接](http://i47.tinypic.com/ddjpd0.png) –

+0

它适用于我添加一个UITextField到NavigationBar ~~ thans Dianz – flypig

0

没关系,得到它固定的由切换的CGRect:

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

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