我在我的应用程序中以编程方式添加了两个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)
答案 0 :(得分:2)
通过这种方式,您可以以编程方式添加约束,并使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))
答案 1 :(得分:1)
这是因为您在所有设备上将其帧原点位置设置为相同。这是屏幕右侧的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)
(...)
答案 2 :(得分:1)
试试这个会很完美。
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。
希望这会对你有所帮助。
答案 3 :(得分:0)
iPhone和iPad设备的坐标系从左上角开始
意思是左上角= x-0,y-0。因此,你的文本字段的x和y可能看起来它位于中心,但实际上它是硬编码的x = 80和y = 120的第一个。 iPhone的宽度为320 (iPhone4,5)
,375 (iPhone6)
,414 (iPhone6+)
且iPad的宽度为768 points.