英文:
How do I change modally presented ViewController to be less presented, like on the image?
问题
这是应该的方式:
这是默认的方式:
我尝试在此视图控制器的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
}
英文:
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)
}
- 这里我有一个带有
UIButton
的ViewController
,以及在点击按钮时调用的函数。 - 此外,我有一个
PopoverViewController
,将在点击按钮时呈现。 - 我创建了一个高度匹配呈现视图减去150.0像素的
.custom
detent。 - 我将我的
customDetent
添加到PopoverViewController
的sheetPresentationController
可以使用的detents
数组中。 - 最后,我呈现了弹出窗口。
以下是说明该过程的一些屏幕截图。
英文:
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 ViewController
s 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("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)
}
- Here I have a
ViewController
with aUIButton
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 ofdetents
that thesheetPresentationController
of myPopoverViewController
can use - Finally I present the popover
Here are a couple of screenshots illustrating the process.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论