SpokaneDude
SpokaneDude

Reputation: 4984

Why is cursor not at top of UITextField?

I have an iPad app (XCode5, ARC, Storyboards, iOS 7). This is the problem (placeholder and cursor in middle of UITextField):

enter image description here

This is the code that creates it:

- (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];

}

The image is from running under iOS7; it works fine when running under iOS 6. As you can see, it kind of works but the cursor/placeholder should be at the top of the UITextField... any ideas why it's not there when running under ios&?

Upvotes: 3

Views: 1529

Answers (1)

Ashok
Ashok

Reputation: 6244

It appears that Apple has changed the default vertical alignment of a UITextField to center (UIControlContentVerticalAlignmentCenter) in iOS 7 versus top (UIControlContentVerticalAlignmentTop) in iOS 6 & previous version.

To fix it in all versions, please add this line -

tf.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;

This will put the cursor and placeholder text at the top. Good catch !!

Upvotes: 8

Related Questions