请尝试以下代码。它可以根据您对iPhone 6S设备的要求完美地工作。当设备屏幕方向更改为横向模式时,我正在计算标签框的x位置和宽度。对于其他设备(例如5S和4S),您需要根据设备的高宽比计算x的位置和宽度。例如,我使用了9和18之类的常数。如果要为不同的设备工作代码,则需要使用比率因子。希望对您有所帮助!
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var theImageView: UIImageView!
@IBOutlet weak var theLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
theLabel.frame.size.width = (theImageView.image?.size.width)!
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}
override func viewDidAppear(animated: Bool) {
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
if (theImageView.image?.size.height)! > (theImageView.image?.size.width)! {
theLabel.frame.origin.x = (view.frame.size.width - (theImageView.image?.size.height)!)/2
theLabel.frame.size.width = (theImageView.image?.size.height)!
}
else {
theLabel.frame.origin.x = ((theImageView.image?.size.height)!/2) - 9
theLabel.frame.size.width = rint(view.frame.size.width) - (theImageView.image?.size.height)! + 18
}
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
theLabel.frame.size.width = theImageView.frame.size.width
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
//theImageView.frame.size.width = (theImageView.image?.size.width)!
//theImageView.frame.size.height = (theImageView.image?.size.height)!
}
func rotated()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
if (theImageView.image?.size.height)! > (theImageView.image?.size.width)! {
theLabel.frame.origin.x = (view.frame.size.width - (theImageView.image?.size.height)!)/2
theLabel.frame.size.width = (theImageView.image?.size.height)!
}
else {
theLabel.frame.origin.x = ((theImageView.image?.size.height)!/2) - 9
theLabel.frame.size.width = rint(view.frame.size.width) - (theImageView.image?.size.height)! + 18
}
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
theLabel.frame.size.width = theImageView.frame.size.width
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句