positioning subclassed UIAlertView window with onscreen keyboard on iOS
I'm using subclassed UIAlertView window as a login prompt in my application (I know it's against Apple guidelines but it will not be submitted to the appstore so it's not a problem).
The code to add text fields and position the window on screen looks like this:
- (id)initWithTitle:(NSString *)title delegate:(id)delegate
{
if (self = [super initWithTitle:title message:@"\n\n\n" delegate:delegate cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil]) {
UITextField *loginTF = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 55.0, 260.0, 25.0)];
// text field settings removed for code clarity
[self addSubview:loginTF];
self.loginTextField = loginTF;
开发者_运维技巧 [loginTF release];
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
// text field settings removed for code clarity
[self addSubview:passwordTF];
self.passwordTextField = passwordTF;
[passwordTF release];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 90.0);
[self setTransform:translate];
}
return self;
}
The problem is with setTransform
call:
on iOS 3.x:
- withoutsetTransform
: The window is displayed centered horizontally and vertically on the screen, with onscreen keyboard displayed partially on top of it - so the whole window is not visible
- with setTransform
: The window is displayed 90 pixels higher, so it is whole visible with onscreen keyboard displayed.
on iOS 4.x however:
- withoutsetTransform
: The window is displayed perfectly fine (visible above the onscreen keyboard)
- with setTransform
: The window is displayed partially above the top border of the screen (90 pixels too high).
What would be the best solution for this issue? Detecting iOS version and calling setTransform
conditionally? Or it's too ugly hack and there is some better way?
Because iOS 4 has been released for the newer devices (and iOS 4.2 for iPad is right around the corner), I would recommend setting the Base SDK to the latest version of iOS 4 and compiling your application with this version. However, if you want your application to be available to original iPhone and iPod Touch users, conditionally using the setTransform
method is the best way.
Whatever (text) you add in the UIAlertView, is shown as label. So if you wanna add textfield, just add the textfield on top of all the labels, and shift down the labels. this is what i have done,
- (void) drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect labelFrame = CGRectMake(0, 0, 0, 0); // init if no label is there
NSArray *views = [self subviews];
for (UIView *view in views){
if ([view isKindOfClass:[UILabel class]]) {
labelFrame = view.frame;
} else {
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + kTextFieldHeight , view.frame.size.width, view.frame.size.height);
}
}
CGRect myFrame = self.frame;
self.textField.frame = CGRectMake(95, labelFrame.origin.y+labelFrame.size.height + 5.0, kTextFieldWidth, kTextFieldHeight);
self.frame = CGRectMake(myFrame.origin.x, myFrame.origin.y, myFrame.size.width, myFrame.size.height + kTextFieldHeight);
}
精彩评论