ios - 以编程方式创建 UILabel、UITextField 等时的图形错误

标签 ios objective-c uitextfield uilabel

我以编程方式创建了一个登录 View 和一个 TableView 。

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

  • 某些字母的字体大小与其他字母不同的文本
  • 一个按钮,其边框最初不显示,只有在您单击它时才会显示。在相同的情况下,按钮的边框可以显示在 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];
}

最佳答案

我无法在脑海中处理您所有的屏幕定位,但在我看来,您的许多字段都重叠:这是故意的吗?也许这就是原因?我通常在代码中创建 View ,但这可能是 IB 的一种情况!

您没有显示 TableView 的任何代码,但是在将 subview 添加到单元格之前我似乎遇到过类似的问题,然后下次使用 DequeueReusableCell 将单元格出队时,您再次添加 subview 而不检查它们是否存在或清除它们。这会导致各种重叠,就像第一个屏幕截图一样。

关于ios - 以编程方式创建 UILabel、UITextField 等时的图形错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1666291/

相关文章:

ios - 相对于堆栈 View 高度约束 UIStackView 的 subview

ios - 带有 CALayers 的 CAAnimation 偶尔会创建闪烁的动画(包括视频)

ios - 如何在 ios 中保存和检索 JSON 响应

ios - 没有名为 'UIUserActivityRestoring' 的类型或协议(protocol)

带键盘的 iPhone/iOS + 自定义工具栏布局

objective-c - Grand Central Dispatch 的简单示例

objective-c - 验证用户 - Objective-C/Cocoa/Sandboxing

objective-c - 顶部带有 uitextfield 的 UIkeyboard 弹出窗口(包括图片)

iphone - UITextField 和 UITextView 的内容到 NSString

ios - 如何在整个应用程序上按返回键快速隐藏键盘?