图形错误,同时以编程方式创建UILabel,UITextField等

时间:2009-11-03 09:44:55

标签: ios objective-c uitextfield uilabel

我以编程方式创建了一个登录视图和一个表视图。

在某些我无法复制的情况下,会出现一些奇怪的视觉错误:

  • 一些字母大小与其他字母不同的文字
  • 最初未显示边框的按钮,但仅在您单击时才显示。在相同的情况下,按钮的边框可以显示在UITextField而不是占位符(见截图)
  • 标签显示不在正确的位置,但在上面提到的文本字段中,颠倒了(我还没设法截图这个)

下面,我添加了两个屏幕截图,其中大部分应用程序都被涂黑了,只显示了错误:

http://uppix.net/b/c/4/dd1e41acb94b7d0ad8eb173772e97.png http://uppix.net/b/f/2/a5993440c07753b851701068330be.png

在这里,您可以看到文本混合了不同的字体大小。

此错误不会定期或可重现。

我添加了以下登录屏幕的代码:

 // Implement loadView to create a view hierarchy programmatically, without 
 // using a nib.
     - (void)loadView {
     UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     UIFont* font = [UIFont systemFontOfSize:14];

 float midLine = 100.0;
 float height = 30.0;
 float borderY = 70.0;
 float borderX = 10.0;
 float width = 320.0 - borderX * 2.0;
 float paddingX = 10.0;
 float paddingY = 5.0;

 CGRect usernameLabelRect = CGRectMake(borderX, borderY, midLine, height);
 CGRect usernameFieldRect = CGRectMake(borderX + midLine + paddingX, borderY, 
                               width - midLine - paddingX - 40 , height);

 CGRect passwordLabelRect = CGRectMake(borderX, borderY + height + paddingY,
                               midLine, height);
 CGRect passwordFieldRect = CGRectMake(borderX + midLine + paddingX, 
                               borderY + height + paddingY, 
                               width - midLine - paddingX - 40, height);

 CGRect loginButtonRect = CGRectMake(borderX + 40, 
                               borderY + height*2 + paddingY*2, width - 80, 
                               height);

 //CGRect warningRect =  CGRectMake(borderX,
 //                         borderY * height * 3.0 + paddingY *3.0, 
 //                         width, height);
 CGRect warningRect =  CGRectMake(borderX, 175, width, 40);

 UILabel* usernameLabel = [[UILabel alloc] initWithFrame:usernameLabelRect];

 usernameLabel.textAlignment = UITextAlignmentRight;
 usernameLabel.font = font;
 usernameLabel.text = @"Username:";

 UILabel* passwordLabel = [[UILabel alloc] initWithFrame:passwordLabelRect];
 passwordLabel.textAlignment = UITextAlignmentRight;
 passwordLabel.font = font;
 passwordLabel.text = @"Password:";


 usernameField = [[UITextField alloc] initWithFrame:usernameFieldRect];
 [usernameField setBorderStyle:UITextBorderStyleRoundedRect];
 //[usernameField setPlaceholder:@"<Username>"];
 usernameField.font = font;
 usernameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
 usernameField.autocorrectionType = UITextAutocorrectionTypeNo;

 passwordField = [[UITextField alloc] initWithFrame:passwordFieldRect];
 passwordField.font = font;
 [passwordField setBorderStyle:UITextBorderStyleRoundedRect];
 //[passwordField setPlaceholder:@"<Password>"];
 passwordField.autocapitalizationType = UITextAutocapitalizationTypeNone;
 passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
 passwordField.secureTextEntry = YES;

 loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 loginButton.frame = loginButtonRect;
 [loginButton setTitle: @"Einloggen" forState:UIControlStateNormal];
 [loginButton setTitleColor:[UIColor blackColor]
                                    forState:UIControlEventTouchDown];
 loginButton.contentVerticalAlignment =
                                    UIControlContentVerticalAlignmentCenter;
 loginButton.contentHorizontalAlignment =
                                    UIControlContentHorizontalAlignmentCenter;
 [loginButton addTarget:self action:@selector(OnLoginClicked:)
                          forControlEvents:UIControlEventTouchUpInside];
 [loginButton setBackgroundColor:[UIColor clearColor]];

 warningLabel = [[UILabel alloc] initWithFrame:warningRect];
 warningLabel.font = font;
 warningLabel.numberOfLines = 3;
 warningLabel.text =
 @"Wenn Sie die Applikation jetzt schließen, so werden Ihre Logindaten verworfen.";

 [myView addSubview:usernameLabel];
 [myView addSubview:passwordLabel];
 [myView addSubview:usernameField];
 [myView addSubview:passwordField];
 [myView addSubview:loginButton];
 [myView addSubview:warningLabel];
 /*
 [usernameLabel release];
 [passwordLabel release];
 [usernameField release];
 [passwordField release];
 [loginButton release];
 [warningLabel release];
 */
 self.view = myView;
 [myView release];
}

2 个答案:

答案 0 :(得分:1)

我不能在你脑海中完成所有的屏幕定位,但它看起来像你的许多字段重叠:这是故意的吗?也许这就是原因?我通常在代码中创建视图,但这可能是IB的一个案例!

您还没有显示表视图的任何代码,但是在将子视图添加到单元格之前我似乎有类似的问题,然后下次使用DequeueReusableCell对单元格进行deQueued时,您再次添加子视图而不检查它们是否存在或清除它们。这会产生各种重叠,就像第一次截图一样。

答案 1 :(得分:0)

问题是我从Not-The-Main-Thread调用UIKit消息,这会导致各种图形问题。通过使用performSelectorOnMainThread来解决它:在我的代码中的几个地方=)