如何以编程方式更改视图控制器宽度大小?

how to change view controller width size programatically?

我正在尝试按照本教程 https://www.youtube.com/watch?v=Hl_Re_KLhcY&t=141s

在不使用 pod 的情况下手动制作 viewcontroller

但是 sideMenuVC(绿色的)的宽度设置准确,我的意思是,sideMenuVC 应该是 MainMenuVC[= 大小的 80% 36=]。因此,如果我从 iPhone 5s 更改为 iPhoneX,autolayout 看起来仍然很完美。但这就是我得到的

sideMenuVC 应该只有 80%,但它在 iPhone 5s

上看起来是全尺寸的

在sideMenuVC中viewDidLoad我想这样设置

class SideMenuVC: UIViewController {

    @IBOutlet var sideMenu: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        sideMenu.frame.width = self.view.frame.width / 2
       }
}

但它不起作用,因为它只获取 属性

所以我想改变这里的宽度

您最希望获得所需输出的是保持视图控制器的宽度不变,但在内部添加具有以下约束的正常 view

  1. 通向 superview,常数值为 0
  2. 顶部到superview,常数值为0
  3. 底到superview,常数值为0
  4. 宽度等于 superview,乘数值为 4:5

您还需要将 UIViewController 的默认 view 属性 的背景颜色设置为 .clear

最后,将新建的 UIViewController 的显示样式设置为 Over Current Context

When presenting a view controller using the UIModalPresentationFullScreen style, UIKit normally removes the views of the underlying view controller after the transition animations finish. You can prevent the removal of those views by specifying the UIModalPresentationOverCurrentContext style instead. You might use that style when the presented view controller has transparent areas that let underlying content show through.