在Ipad上用Single StackView替换Double StackView?

时间:2018-03-14 03:49:45

标签: ios swift uiviewcontroller stackview

我有一个故事板,其中有一个单独的视图控制器用于UIView。在那个UIView我必须在IPad的情况下在一行中加载10个按钮,否则我必须在两行中加载10个按钮。我尝试过使用2个单独的xib文件并静态加载它们但是我想用编程控制器以编程方式进行编译。Storyboard Image 我添加了故事板的截图。

任何人都可以指导我动态更改视图。这是按钮的新ViewController的代码。

import Foundation

 class TNPSRatingsViewController: UIViewController {

    var selectedScoreButton: TNPSScoreButton?
    var scoreSelectedOnce = false

    @IBOutlet var buttons: [TNPSScoreButton]!


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.modifyPositionOfTNPSButton()
    }

    override func viewWillTransition(to size: CGSize,
                                     with coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animate(alongsideTransition: nil, completion: ({ [unowned self] _ in
            self.modifyPositionOfTNPSButton()

        }))
    }


    // MARK: - Private functions
    fileprivate func modifyPositionOfTNPSButton() {
        if UIApplication.shared.delegate?.window??.traitCollection.horizontalSizeClass == .compact {
            //
            //
        } else {
            //
            //

        }
    }



}

先谢谢

2 个答案:

答案 0 :(得分:0)

如何使用UICollectionView?

在iPad上

,使用1行10项 在iPhone上使用2行5项

您可以坚持查看控制器包含,或直接使用您的第一个屏幕作为委托/数据源实现uicollectionview。

答案 1 :(得分:0)

尝试创建一个可以告诉当前设备是iPad或iPhone的课程

class Env
{
    //Check if its iPad
    static var iPad: Bool
    {
        return UIDevice.current.userInterfaceIdiom == .pad
    }
}

现在,您需要在containerView中添加子视图

只需将该类用作:

class ViewController: UIViewController {

    var myCustomContainerView = UIView()
    var subviewCreatedXIBForiPad = UIView()
    var subviewCreatedXIBForiPhone = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        Env.iPad ? myCustomContainerView.addSubview(subviewCreatedXIBForiPad) : myCustomContainerView.addSubview(subviewCreatedXIBForiPhone)
    }
}