屏幕旋转后 UITextField NSForegroundColor 变为透明 Swift

UITextField NSForegroundColor turns transparent after screen rotation Swift

我遇到一个问题,UITextField 字体前景色在屏幕旋转后变为透明。下面是我的代码。

let TextAttributes = [
    NSStrokeColorAttributeName : UIColor.blackColor(),
    NSForegroundColorAttributeName : UIColor.whiteColor(),
    NSFontAttributeName : UIFont(name: "HelveticaNeue-CondensedBlack", size: 40)!,
    NSStrokeWidthAttributeName : 3.0
]

//In viewDidLoad
textFieldTop.delegate = textFieldTopDelegate
textFieldTop.text = "TOP"
textFieldTop.frame = CGRectMake(0, 88, self.view.frame.width, 44)
textFieldTop.defaultTextAttributes = TextAttributes
self.view.addSubview(textFieldTop)
textFieldTop.textAlignment = NSTextAlignment.Center

textFieldBottom.delegate = textFieldBottomDelegate
textFieldBottom.text = "BOTTOM"
textFieldBottom.frame = CGRectMake(0, self.view.frame.height - 88, self.view.frame.width, 44)
textFieldBottom.defaultTextAttributes = TextAttributes
self.view.addSubview(textFieldBottom)
textFieldBottom.textAlignment = NSTextAlignment.Center

//in didRotateFromInterfaceOrientation
textFieldTop.frame = CGRectMake(0, 88, self.view.frame.width, 44)
textFieldTop.defaultTextAttributes = TextAttributes
self.view.addSubview(textFieldTop)
textFieldTop.textAlignment = NSTextAlignment.Center
textFieldBottom.frame = CGRectMake(0, self.view.frame.height - 88, self.view.frame.width, 44)
textFieldBottom.defaultTextAttributes = TextAttributes
self.view.addSubview(textFieldBottom)
textFieldBottom.textAlignment = NSTextAlignment.Center

如何确保前景文字颜色在旋转后不会自动变透明?

谢谢!

更新

我意识到我的问题不是来自旋转。由于某些原因,前景色是透明的,无法设置。

每次调用 didRotateFromInterfaceOrientation 时,您都会向自己添加 textFieldTop 和 textFieldBottom,在 didRotateFromInterfaceOrientation 中,您需要做的就是移动和调整字段大小:

textFieldTop.frame = CGRectMake(0, 88, self.view.frame.width, 44)
textFieldBottom.frame = CGRectMake(0, self.view.frame.height - 88,  self.view.frame.width, 44)

原来我需要将 NSStrokeWidthAttributeName 设置为 -3