ios - 为什么这个 UITextField 是预期的两倍大?

标签 ios objective-c

我正在向我的应用添加一个 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/

相关文章:

ios - 为什么 UITableViewCell 首先显示以前的值,然后才更新自身?

ios - 旋转动画直到 UIViewController 消失

iphone - 将视频保存到照片库 - iPhone SDK

ios - NSLog 在第三个变量后停止打印

iphone - 当前时间在 HH :MM:SS am/pm format?

ios - Core Image 和 GPUImage 之间的主要区别

ios - 当我在 iOS 中创建 ScrollView 的导出时,按钮单击在 ScrollView 中没有响应

ios - 将当前应用程序的构建版本输出为字符串 - Xamarin.iOS

ios - 仅在 iPhone 7 之前的设备上使用前置摄像头时崩溃

ios - 在 iOS 7 中使用 Assets URL 获取图像 (UIImage)