我以編程方式創建了一個登錄視圖和一個表視圖。以編程方式創建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];
}
這裏是另一張截圖: http://uppix.net/b/f/2/a5993440c07753b851701068330be.png 正如你所看到的,按鈕在這種情況下,「Einloggen」沒有顯示按鈕。通常它有。您還可以看到用戶名標籤的佔位符顯示「邊框」,而不是其實際佔位符 – Tomen 2009-11-03 09:45:24
您發佈的登錄屏幕是使用界面生成器的主要示例。你基本上只是放置一些控件並設置了很多可以由Interface Builder處理的屬性。爲登錄視圖製作一個獨立的Xib並在需要時加載它,這是要走的路。 在代碼中設置自己的視圖很容易出錯,並且視覺效果更好。由於重疊控制等原因,更容易預先發現問題。 爲什麼你需要在代碼中設置它? – 2009-11-04 08:31:21