2009-11-03 60 views
0

我以编程方式创建了一个登录视图和一个表视图。以编程方式创建UILabel,UITextField等时的图形错误

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

  • 与具有不同的字体大小比其他一些字母文字
  • 在边境最初没有显示,但只有当A键你点击它。在同一种情况下,按钮的边框可以显示在UITextField中,而不是占位符(请参阅屏幕截图)
  • 标签显示不在正确的位置,但在上述文本框中颠倒(我做了不设法截图这一个尚未)

下面,我添加了两个截图与大多数应用程序的昏了过去,只显示错误:

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

^h您可以看到文字与不同的字体大小混合在一起。

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

我加了下面的登录界面的代码:

// 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]; 
} 
+0

这里是另一张截图: http://uppix.net/b/f/2/a5993440c07753b851701068330be.png 正如你所看到的,按钮在这种情况下,“Einloggen”没有显示按钮。通常它有。您还可以看到用户名标签的占位符显示“边框”,而不是其实际占位符 – Tomen 2009-11-03 09:45:24

+0

您发布的登录屏幕是使用界面生成器的主要示例。你基本上只是放置一些控件并设置了很多可以由Interface Builder处理的属性。为登录视图制作一个独立的Xib并在需要时加载它,这是要走的路。 在代码中设置自己的视图很容易出错,并且视觉效果更好。由于重叠控制等原因,更容易预先发现问题。 为什么你需要在代码中设置它? – 2009-11-04 08:31:21

回答

0

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

1

我不能工作的所有屏幕定位在我的头上,但它看起来对我来说,你的许多领域的重叠:这是故意的?也许这是原因?我通常在代码中创建视图,但这可能是IB的一种情况!

你没有显示任何代码的表视图,但我似乎在你添加子视图到一个单元格的地方,然后下一次单元DeQueued与DequeueReusableCell deQueued再次添加子视图,而不检查它们是否是那里或清理它们。这会导致各种重叠,就像第一个截图一样。

相关问题