How do I change modally presented ViewController to be less presented, like on the image?

huangapple go评论140阅读模式
英文:

How do I change modally presented ViewController to be less presented, like on the image?

问题

这是应该的方式:

This is how it should be

这是默认的方式:

How it is by default

我尝试在此视图控制器的viewDidLoad方法中更改框架:

override func viewDidLoad() {
    super.viewDidLoad()
    // 在加载视图后进行任何额外的设置。
    let height = view.frame.height
    let width = view.frame width
    let yOffset: CGFloat = +100.0 // 向下移动 100 点
    let newFrame = CGRect(x: 0, y: yOffset, width: width, height: height)
    view.frame = newFrame
}
英文:

This is how it should be

How it is by default

I have tried changing the frame in viewDidLoad of this vc

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let height = view.frame.height
    let width = view.frame.width
    let yOffset: CGFloat = +100.0 // Move down by 100 points
    let newFrame = CGRect(x: 0, y: yOffset, width: width, height: height)
    view.frame = newFrame
}


</details>


# 答案1
**得分**: 0

你可以尝试查看[UISheetPresentationController](https://developer.apple.com/documentation/uikit/uisheetpresentationcontroller)和`Detents`的概念,它控制呈现视图(sheet)应占用多少空间。

最初,您可以使用`.large`和`.medium`大小的detents,但在iOS 16中引入了`.custom` detents的概念,允许您自己控制sheet的大小。

**注意**:当您 _呈现_ 视图时,需要设置视图的 `sheetPresentationController`。因此,您不会在 _呈现的_ `ViewController` 生命周期的一部分设置它,而是在 _呈现_ 的 `ViewController` 逻辑的一部分设置它。

## 一个示例

```swift
@IBAction func didTapButton(_ sender: UIButton) {
    let viewController = PopoverViewController()

    let customDetent = UISheetPresentationController.Detent.custom(identifier: .init("myCustomDetent")) { [weak self] context in
        guard let self = self else { return 0.0 }
        return self.view.frame.height - 150.0
    }
    
    if let sheet = viewController.sheetPresentationController {
        sheet.detents = [customDetent]
    }

    present(viewController, animated: true)
}
  • 这里我有一个带有UIButtonViewController,以及在点击按钮时调用的函数。
  • 此外,我有一个PopoverViewController,将在点击按钮时呈现。
  • 我创建了一个高度匹配呈现视图减去150.0像素的.custom detent。
  • 我将我的customDetent添加到PopoverViewControllersheetPresentationController可以使用的detents数组中。
  • 最后,我呈现了弹出窗口。

以下是说明该过程的一些屏幕截图。

How do I change modally presented ViewController to be less presented, like on the image?
How do I change modally presented ViewController to be less presented, like on the image?

英文:

You could try looking into UISheetPresentationController and the concept of Detents which controls how much space a presented view (a sheet) should take up.

Initially you had the ability to use .large and .medium sized detents but in iOS 16 the concept of .custom detents was introduced, allowing you to control the sheet size yourself.

Note you need to set up the sheetPresentationController of a view when you present it. So you won't set this up as part of the presented ViewControllers lifecycle, but instead as part of the presenting ViewController logic.

An Example

@IBAction func didTapButton(_ sender: UIButton) {
    let viewController = PopoverViewController()

    let customDetent = UISheetPresentationController.Detent.custom(identifier: .init(&quot;myCustomDetent&quot;)) { [weak self] context in
        guard let self = self else { return 0.0 }
        return self.view.frame.height - 150.0
    }
    
    if let sheet = viewController.sheetPresentationController {
        sheet.detents = [ customDetent ]
    }

    present(viewController, animated: true)
}
  • Here I have a ViewController with a UIButton and a function that is called when that button is tapped.
  • Furthermore I have a PopoverViewController that will be presented when the button is tapped.
  • I create a .custom detent with a height matching the presenting view minus 150.0 pixels.
  • I add my customDetent to the array of detents that the sheetPresentationController of my PopoverViewController can use
  • Finally I present the popover

Here are a couple of screenshots illustrating the process.

How do I change modally presented ViewController to be less presented, like on the image?
How do I change modally presented ViewController to be less presented, like on the image?

huangapple
  • 本文由 发表于 2023年3月9日 19:31:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定