gpt4 book ai didi

ios - 如何将此 objective-c traitCollection 转换为 swift?

转载 作者:可可西里 更新时间:2023-11-01 01:05:37 27 4
gpt4 key购买 nike

我正在尝试通过此链接将这段代码翻译成 Objective-C ,Sizing class for iPad portrait and Landscape Modes

这是我要翻译的代码:

@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.
}

我想我几乎拥有所有功能,但最后一个功能我无法弄清楚它的含义。任何帮助将不胜感激。

最佳答案

我认为你在最后一个函数中缺少“override”,尝试用下面的代码替换:

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.
}
}

关于ios - 如何将此 objective-c traitCollection 转换为 swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com