我正在尝试通过此链接Sizing class for iPad portrait and Landscape Modes
在目标C中翻译此代码以下是我要翻译的代码:
@interface RDTraitCollectionOverrideViewController : UIViewController {
BOOL _willTransitionToPortrait;
UITraitCollection *_traitCollection_CompactRegular;
UITraitCollection *_traitCollection_AnyAny;
}
@end
@implementation RDTraitCollectionOverrideViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpReferenceSizeClasses];
}
- (void)setUpReferenceSizeClasses {
UITraitCollection *traitCollection_hCompact = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
UITraitCollection *traitCollection_vRegular = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
_traitCollection_CompactRegular = [UITraitCollection traitCollectionWithTraitsFromCollections:@[traitCollection_hCompact, traitCollection_vRegular]];
UITraitCollection *traitCollection_hAny = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassUnspecified];
UITraitCollection *traitCollection_vAny = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassUnspecified];
_traitCollection_AnyAny = [UITraitCollection traitCollectionWithTraitsFromCollections:@[traitCollection_hAny, traitCollection_vAny]];
}
-(void)viewWillAppear:(BOOL)animated {
_willTransitionToPortrait = self.view.frame.size.height > self.view.frame.size.width;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
_willTransitionToPortrait = size.height > size.width;
}
-(UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController {
UITraitCollection *traitCollectionForOverride = (_willTransitionToPortrait) ? _traitCollection_CompactRegular : _traitCollection_AnyAny;
return traitCollectionForOverride;
}
@end
这就是我所拥有的,请记住,我在类声明下重命名了一些初始变量:
import UIKit
class ViewController: UIViewController {
var willTransitionToPortrait:Bool = false
var compactRegular = UITraitCollection()
var anyAny = UITraitCollection()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUpReferenceSizeClasses()
}
func setUpReferenceSizeClasses() {
var traitCollection_hCompact = UITraitCollection(horizontalSizeClass: UIUserInterfaceSizeClass.Compact)
var traitCollection_vRegular = UITraitCollection(verticalSizeClass: UIUserInterfaceSizeClass.Regular)
compactRegular = UITraitCollection(traitsFromCollections: [traitCollection_hCompact, traitCollection_vRegular])
var traitCollection_hAny = UITraitCollection(horizontalSizeClass: UIUserInterfaceSizeClass.Unspecified)
var traitCollection_vAny = UITraitCollection(verticalSizeClass: UIUserInterfaceSizeClass.Unspecified)
anyAny = UITraitCollection(traitsFromCollections: [traitCollection_hAny, traitCollection_vAny])
}
override func viewWillAppear(animated: Bool) {
willTransitionToPortrait = self.view.frame.size.height > self.view.frame.size.width
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
willTransitionToPortrait = size.height > size.width
}
func overrideTraitCollectionForChildViewController(UIViewController) -> childViewController {
var traitCollectionForOverride = (willTransitionToPortrait) ? compactRegular : anyAny
return traitCollectionForOverride;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我认为我几乎拥有它,但最后一个功能我无法弄清楚它的翻译内容。任何帮助将不胜感激。
答案 0 :(得分:3)
我认为你错过了&#34;覆盖&#34;在最后一个函数中,尝试用下面的代码替换:
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection! {
//
}
以下是完整的代码:
import UIKit
class ViewController: UIViewController {
var willTransitionToPortrait:Bool = false
var compactRegular = UITraitCollection()
var anyAny = UITraitCollection()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setUpReferenceSizeClasses()
}
func setUpReferenceSizeClasses() {
var traitCollection_hCompact = UITraitCollection(horizontalSizeClass: UIUserInterfaceSizeClass.Compact)
var traitCollection_vRegular = UITraitCollection(verticalSizeClass: UIUserInterfaceSizeClass.Regular)
compactRegular = UITraitCollection(traitsFromCollections: [traitCollection_hCompact, traitCollection_vRegular])
var traitCollection_hAny = UITraitCollection(horizontalSizeClass: UIUserInterfaceSizeClass.Unspecified)
var traitCollection_vAny = UITraitCollection(verticalSizeClass: UIUserInterfaceSizeClass.Unspecified)
anyAny = UITraitCollection(traitsFromCollections: [traitCollection_hAny, traitCollection_vAny])
}
override func viewWillAppear(animated: Bool) {
willTransitionToPortrait = self.view.frame.size.height > self.view.frame.size.width
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
willTransitionToPortrait = size.height > size.width
}
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection! {
//
var traitCollectionForOverride = (willTransitionToPortrait) ? compactRegular : anyAny
return traitCollectionForOverride;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
&#13;