为什么游标不在UITextField的顶部?

时间:2013-10-22 16:58:03

标签: ios objective-c uitextfield

我有一个iPad应用程序(XCode5,ARC,Storyboards,iOS 7)。这是问题(占位符和光标位于UITextField的中间):

enter image description here

这是创建它的代码:

- (IBAction)bSendFeedback:(UIButton *)sender {

//  make the popover
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 450, 500)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0];  //  frame color?
popoverContent.view = popoverView;

//resize the popover view shown in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(450, 500);

//  add the UITextfield to the popover
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 450, 500)];
[tf becomeFirstResponder];
tf.delegate = self;
tf.tag = kSendFeedback;
tf.placeholder = @" Enter your comments here";
tf.backgroundColor = UIColorFromRGB(0xFFFFE0);
tf.returnKeyType = UIReturnKeyDone;  //  make return key read "Done"
[popoverView addSubview:tf];

//  if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
    [popoverController dismissPopoverAnimated:YES];
}

//create a popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:((UIButton *)oSendFeedback).frame inView:self.view
                 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

图像来自iOS7下运行; 在iOS 6下运行时效果很好。 正如你所看到的,它有点工作,但光标/占位符应该位于UITextField的顶部...任何想法为什么它在ios&?下运行时不存在?

1 个答案:

答案 0 :(得分:8)

似乎Apple已将UITextField的默认垂直对齐方式更改为iOS 7中的中心(UIControlContentVerticalAlignmentCenter)与iOS 6中的顶级(UIControlContentVerticalAlignmentTop)。以前的版本。

要在所有版本中修复它,请添加此行 -

tf.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

这会将光标和占位符文本放在顶部。好抓!!!