positioning subclassed UIAlertView window with onscreen keyboard on iOS

天大地大妈咪最大 提交于 2019-12-11 05:14:17

问题


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:
- without setTransform: 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:
- without setTransform: 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?


回答1:


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.




回答2:


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);

}


来源:https://stackoverflow.com/questions/4184824/positioning-subclassed-uialertview-window-with-onscreen-keyboard-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!