我正在为我的应用添加UITextField
,它应该是300x30但最终却是600x60。这是我到目前为止...在我的应用代表中:
- (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
应该是它的两倍?
答案 0 :(得分:1)
如果您的设备有视网膜显示,那么[[UIScreen mainScreen] bounds]
将返回两倍大的矩形。但UIView和UITextField的像素仍在其他坐标系中计算。
例如,如果你有iPhone 4,你的矩形将是{{0,0},{640,960}}。
只需将mainScreen.bounds矩形的高度和宽度除以2即可。
UPD:这个数字2来自[[UIScreen mainScreen] scale]属性。
答案 1 :(得分:0)
在iOS中,您定义的框架矩形不是以像素为单位,而是以点为单位。因此,如果您定义一个0,0,300,30的帧,它将在视网膜显示屏上显示0,0,600,60: - )
答案 2 :(得分:0)
检查textfield.autoresiging属性的autoresiging属性textfield的宽度和高度不应该有所不同,希望它可以解决你的问题。