我正在向我的应用添加一个 UITextField
,它应该是 300x30,但最终变成了 600x60。这是我到目前为止所拥有的...在我的应用程序委托(delegate)中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
OpenGLViewController* controller = [[OpenGLViewController alloc] init];
[self.window setRootViewController:controller];
[self.window addSubview:controller.view];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[self.window makeKeyAndVisible];
return YES;
}
在 OpenGLViewController
中:
- (void)loadView {
self.view = [[OpenGLView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"Search...";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.returnKeyType = UIReturnKeySearch;
textField.delegate = self;
[self.view addSubview:textField];
}
在我的 OpenGlView
的初始化中(我认为这不会影响大小):
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setupLayer];
[self setupContext];
[self setupRenderBuffer];
[self setupFrameBuffer];
[self compileShaders];
glViewport(0, 0, frame.size.width, frame.size.height);
}
return self;
}
为什么这个 UITextField
是它应该的两倍大?
最佳答案
如果您的设备有视网膜显示屏,则 [[UIScreen mainScreen] bounds]
将返回两倍大的矩形。但是 UIView 和 UITextField 的像素仍然是在另一个坐标系中计算的。
例如,如果您有 iPhone 4,则您的矩形将为 {{0,0},{640,960}}。
只需将 mainScreen.bounds 矩形的高度和宽度除以 2。
UPD:这个数字 2 来自 [[UIScreen mainScreen] scale] 属性。
关于ios - 为什么这个 UITextField 是预期的两倍大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704858/