UITextFields 在我的 iPad 中的位置不正确......为什么?
UITextFields are not positioned correctly in my iPad.....Why?
我在我的应用程序中以编程方式添加了两个 UITextField,但它们在 iPad 上的位置不正确。在 iPhone 上有我希望它们出现的位置,但对于 iPad 它们的位置不是我希望它们出现的位置。如何让它们与 iPhone 的位置相同?
textFieldOne.frame = CGRectMake(80, 120, 160, 40)
textFieldOne.borderStyle = UITextBorderStyle.Line
textFieldOne.textColor = UIColor.whiteColor()
textFieldOne.autocorrectionType = UITextAutocorrectionType.Yes
textFieldOne.keyboardType = UIKeyboardType.NumberPad
textFieldOne.delegate = self
textFieldOne.backgroundColor = UIColor.clearColor()
textFieldOne.returnKeyType = UIReturnKeyType.Done
textFieldOne.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
self.view?.addSubview(textFieldOne)
textFieldTwo.frame = CGRectMake(80, 210, 160, 40)
textFieldTwo.borderStyle = UITextBorderStyle.Line
textFieldTwo.textColor = UIColor.whiteColor()
textFieldTwo.autocorrectionType = UITextAutocorrectionType.Yes
textFieldTwo.keyboardType = UIKeyboardType.NumberPad
textFieldTwo.delegate = self
textFieldTwo.backgroundColor = UIColor.clearColor()
textFieldTwo.returnKeyType = UIReturnKeyType.Done
textFieldTwo.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
self.view?.addSubview(textFieldTwo)
iPhone和iPad设备的坐标系从左上角开始
表示左上角 = x-0,y-0。所以你的文本字段的 x 和 y 可能看起来它在中心但实际上它是硬编码的 x = 80 和 y = 120 第一个。 iPhone 的宽度为 320 (iPhone4,5)
、375 (iPhone6)
、414 (iPhone6+)
,iPad 的宽度为 768 points.
这是因为您将它们的框架原点位置设置为在所有设备上都相同。即距屏幕右侧 80 个单位,距离顶部 210 个单位。在 iPhone 上,它可能看起来居中,但在屏幕更大的 iPad 上却不是。如果您想避免以编程方式添加约束,请尝试将其更改为这样的内容:
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
textFieldOne.frame = CGRectMake(80, 120, 160, 40)
textFieldOne.center = CGPointMake(screenWidth/2, 120)
(...)
textFieldTwo.frame = CGRectMake(80, 210, 160, 40)
textFieldTwo.center = CGPointMake(screenWidth/2, 120)
(...)
或
textFieldOne.frame = CGRectMake(80, 120, 160, 40)
textFieldOne.frame.origin.x = screenWidth/2 - (textFieldOne.frame.width/2)
(...)
textFieldTwo.frame = CGRectMake(80, 210, 160, 40)
textFieldTwo.frame.origin.x = screenWidth/2 - (textFieldTwo.frame.width/2)
(...)
或...
textFieldOne.frame = CGRectMake(screenWidth/2 - 80, 120, 160, 40)
(...)
textFieldTwo.frame = CGRectMake(screenWidth/2 - 80, 210, 160, 40)
(...)
试试这个,它会完美的。
var X : CGFloat = 0;
var Y : CGFloat = 0;
var Width : CGFloat = 0;
var Height : CGFloat = 0;
var ScreenWidth : CGFloat = UIScreen.mainScreen().bounds.width;
var ScreenHeight : CGFloat = UIScreen.mainScreen().bounds.height;
if UIDevice.currentDevice().userInterfaceIdiom == .Pad
{
// for ipad.
X = (ScreenWidth * <X position you want in ipad>) / 768;
Y = (ScreenHeight * <Y position you want in ipad>) / 1024;
Width = (ScreenWidth * <width you want in ipad>) / 768;
Height = (ScreenHeight * <height you want in ipad>) / 1024;
}
else
{
//for iphone.
X = (ScreenWidth * <X position you want in iphone 5>) / 320;
Y = (ScreenHeight * <Y position you want in iphone 5>) / 568;
Width = (ScreenWidth * <width you want in iphone 5>) / 320;
Height = (ScreenHeight * <height you want in iphone 5>) / 568;
}
上面的代码让你的框架只需将它传递给 CGRectmake 方法,你就完美了 UI。
希望对您有所帮助。
通过这种方式,您可以通过编程方式添加约束,它会使您的 textFields
水平居中。
textFieldOne.borderStyle = UITextBorderStyle.Line
textFieldOne.textColor = UIColor.whiteColor()
textFieldOne.autocorrectionType = UITextAutocorrectionType.Yes
textFieldOne.keyboardType = UIKeyboardType.NumberPad
textFieldOne.delegate = self
textFieldOne.backgroundColor = UIColor.clearColor()
textFieldOne.returnKeyType = UIReturnKeyType.Done
textFieldOne.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
textFieldOne.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view?.addSubview(textFieldOne)
//center it horizontally
let horizontalConstraintForFirst = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraintForFirst)
//top space
let topConstraintForFirst = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 120)
view.addConstraint(topConstraintForFirst)
//width
let widthConstraint = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 160)
textFieldOne.addConstraint(widthConstraint)
//height
let heightConstraint = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
textFieldOne.addConstraint(heightConstraint)
textFieldTwo.borderStyle = UITextBorderStyle.Line
textFieldTwo.textColor = UIColor.whiteColor()
textFieldTwo.autocorrectionType = UITextAutocorrectionType.Yes
textFieldTwo.keyboardType = UIKeyboardType.NumberPad
textFieldTwo.delegate = self
textFieldTwo.backgroundColor = UIColor.clearColor()
textFieldTwo.returnKeyType = UIReturnKeyType.Done
textFieldTwo.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
textFieldTwo.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view?.addSubview(textFieldTwo)
//center it horizontally
let horizontalConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraintForSecond)
//top space
let topConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 210)
view.addConstraint(topConstraintForSecond)
//width
let widthConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 160)
textFieldTwo.addConstraint(widthConstraintForSecond)
//height
let heightConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
textFieldTwo.addConstraint(heightConstraintForSecond)
更新:
对于 SpriteKit 替换此行:
let topConstraintForFirst = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 120)
view.addConstraint(topConstraintForFirst)
let topConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 210)
view.addConstraint(topConstraintForSecond)
(给你和错误)这一行:
//top space for first TextField
self.view?.addConstraint(NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 120))
//top space for second TextField
self.view?.addConstraint(NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 210))
我在我的应用程序中以编程方式添加了两个 UITextField,但它们在 iPad 上的位置不正确。在 iPhone 上有我希望它们出现的位置,但对于 iPad 它们的位置不是我希望它们出现的位置。如何让它们与 iPhone 的位置相同?
textFieldOne.frame = CGRectMake(80, 120, 160, 40)
textFieldOne.borderStyle = UITextBorderStyle.Line
textFieldOne.textColor = UIColor.whiteColor()
textFieldOne.autocorrectionType = UITextAutocorrectionType.Yes
textFieldOne.keyboardType = UIKeyboardType.NumberPad
textFieldOne.delegate = self
textFieldOne.backgroundColor = UIColor.clearColor()
textFieldOne.returnKeyType = UIReturnKeyType.Done
textFieldOne.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
self.view?.addSubview(textFieldOne)
textFieldTwo.frame = CGRectMake(80, 210, 160, 40)
textFieldTwo.borderStyle = UITextBorderStyle.Line
textFieldTwo.textColor = UIColor.whiteColor()
textFieldTwo.autocorrectionType = UITextAutocorrectionType.Yes
textFieldTwo.keyboardType = UIKeyboardType.NumberPad
textFieldTwo.delegate = self
textFieldTwo.backgroundColor = UIColor.clearColor()
textFieldTwo.returnKeyType = UIReturnKeyType.Done
textFieldTwo.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
self.view?.addSubview(textFieldTwo)
iPhone和iPad设备的坐标系从左上角开始
表示左上角 = x-0,y-0。所以你的文本字段的 x 和 y 可能看起来它在中心但实际上它是硬编码的 x = 80 和 y = 120 第一个。 iPhone 的宽度为 320 (iPhone4,5)
、375 (iPhone6)
、414 (iPhone6+)
,iPad 的宽度为 768 points.
这是因为您将它们的框架原点位置设置为在所有设备上都相同。即距屏幕右侧 80 个单位,距离顶部 210 个单位。在 iPhone 上,它可能看起来居中,但在屏幕更大的 iPad 上却不是。如果您想避免以编程方式添加约束,请尝试将其更改为这样的内容:
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
textFieldOne.frame = CGRectMake(80, 120, 160, 40)
textFieldOne.center = CGPointMake(screenWidth/2, 120)
(...)
textFieldTwo.frame = CGRectMake(80, 210, 160, 40)
textFieldTwo.center = CGPointMake(screenWidth/2, 120)
(...)
或
textFieldOne.frame = CGRectMake(80, 120, 160, 40)
textFieldOne.frame.origin.x = screenWidth/2 - (textFieldOne.frame.width/2)
(...)
textFieldTwo.frame = CGRectMake(80, 210, 160, 40)
textFieldTwo.frame.origin.x = screenWidth/2 - (textFieldTwo.frame.width/2)
(...)
或...
textFieldOne.frame = CGRectMake(screenWidth/2 - 80, 120, 160, 40)
(...)
textFieldTwo.frame = CGRectMake(screenWidth/2 - 80, 210, 160, 40)
(...)
试试这个,它会完美的。
var X : CGFloat = 0;
var Y : CGFloat = 0;
var Width : CGFloat = 0;
var Height : CGFloat = 0;
var ScreenWidth : CGFloat = UIScreen.mainScreen().bounds.width;
var ScreenHeight : CGFloat = UIScreen.mainScreen().bounds.height;
if UIDevice.currentDevice().userInterfaceIdiom == .Pad
{
// for ipad.
X = (ScreenWidth * <X position you want in ipad>) / 768;
Y = (ScreenHeight * <Y position you want in ipad>) / 1024;
Width = (ScreenWidth * <width you want in ipad>) / 768;
Height = (ScreenHeight * <height you want in ipad>) / 1024;
}
else
{
//for iphone.
X = (ScreenWidth * <X position you want in iphone 5>) / 320;
Y = (ScreenHeight * <Y position you want in iphone 5>) / 568;
Width = (ScreenWidth * <width you want in iphone 5>) / 320;
Height = (ScreenHeight * <height you want in iphone 5>) / 568;
}
上面的代码让你的框架只需将它传递给 CGRectmake 方法,你就完美了 UI。
希望对您有所帮助。
通过这种方式,您可以通过编程方式添加约束,它会使您的 textFields
水平居中。
textFieldOne.borderStyle = UITextBorderStyle.Line
textFieldOne.textColor = UIColor.whiteColor()
textFieldOne.autocorrectionType = UITextAutocorrectionType.Yes
textFieldOne.keyboardType = UIKeyboardType.NumberPad
textFieldOne.delegate = self
textFieldOne.backgroundColor = UIColor.clearColor()
textFieldOne.returnKeyType = UIReturnKeyType.Done
textFieldOne.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
textFieldOne.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view?.addSubview(textFieldOne)
//center it horizontally
let horizontalConstraintForFirst = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraintForFirst)
//top space
let topConstraintForFirst = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 120)
view.addConstraint(topConstraintForFirst)
//width
let widthConstraint = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 160)
textFieldOne.addConstraint(widthConstraint)
//height
let heightConstraint = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
textFieldOne.addConstraint(heightConstraint)
textFieldTwo.borderStyle = UITextBorderStyle.Line
textFieldTwo.textColor = UIColor.whiteColor()
textFieldTwo.autocorrectionType = UITextAutocorrectionType.Yes
textFieldTwo.keyboardType = UIKeyboardType.NumberPad
textFieldTwo.delegate = self
textFieldTwo.backgroundColor = UIColor.clearColor()
textFieldTwo.returnKeyType = UIReturnKeyType.Done
textFieldTwo.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
textFieldTwo.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view?.addSubview(textFieldTwo)
//center it horizontally
let horizontalConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraintForSecond)
//top space
let topConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 210)
view.addConstraint(topConstraintForSecond)
//width
let widthConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 160)
textFieldTwo.addConstraint(widthConstraintForSecond)
//height
let heightConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40)
textFieldTwo.addConstraint(heightConstraintForSecond)
更新:
对于 SpriteKit 替换此行:
let topConstraintForFirst = NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 120)
view.addConstraint(topConstraintForFirst)
let topConstraintForSecond = NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: topLayoutGuide, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 210)
view.addConstraint(topConstraintForSecond)
(给你和错误)这一行:
//top space for first TextField
self.view?.addConstraint(NSLayoutConstraint(item: textFieldOne, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 120))
//top space for second TextField
self.view?.addConstraint(NSLayoutConstraint(item: textFieldTwo, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 210))