Hello,
I am a new developer and it is my first time trying to build an iMessage Extension app.
I am having an issue with presenting a SwiftUI view using a hosting controller in iOS 16. My code works on earlier OS versions, but for some reason when I updated to iOS 16 the view constraints seem off.
I am running the app on an iPhone 13 on iOS 16.03 and what I encounter is the following: When I open the app the view appears all the way at the top of the screen past the safe area.
I was able to replicate the issue using the IceCreamBuilderMessagesExtension project in apple docs (https://developer.apple.com/documentation/messages/icecreambuilder_building_an_imessage_extension).
I got rid of most of the code in the IceCreamBuilder project to try and isolate just the issue with presenting a SwiftUI view. Here is my code in the IceCreamBuilder project:
/*
The root view controller shown by the Messages app.
*/
import UIKit
import Messages
import SwiftUI
class MessagesViewController: MSMessagesAppViewController {
// MARK: Properties
override func willBecomeActive(with conversation: MSConversation) {
super.willBecomeActive(with: conversation)
// Present the view controller appropriate for the conversation and presentation style.
presentViewController(for: conversation, with: presentationStyle)
}
// MARK: MSMessagesAppViewController overrides
override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
super.willTransition(to: presentationStyle)
// Hide child view controllers during the transition.
removeAllChildViewControllers()
}
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
super.didTransition(to: presentationStyle)
// Present the view controller appropriate for the conversation and presentation style.
guard let conversation = activeConversation else { fatalError("Expected an active converstation") }
presentViewController(for: conversation, with: presentationStyle)
}
// MARK: Child view controller presentation
/// - Tag: PresentViewController
private func presentViewController(for conversation: MSConversation, with presentationStyle: MSMessagesAppPresentationStyle) {
// Remove any child view controllers that have been presented.
removeAllChildViewControllers()
let controller = UIHostingController(rootView: TestView())
addChild(controller)
controller.view.frame = view.bounds
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leftAnchor.constraint(equalTo: view.leftAnchor),
controller.view.rightAnchor.constraint(equalTo: view.rightAnchor),
controller.view.topAnchor.constraint(equalTo: view.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
controller.didMove(toParent: self)
}
// MARK: Convenience
private func removeAllChildViewControllers() {
for child in children {
child.willMove(toParent: nil)
child.view.removeFromSuperview()
child.removeFromParent()
}
}
}
// TestView.swift
// IceCreamBuilderMessagesExtension
import SwiftUI
struct TestView: View {
var body: some View {
VStack {
Text("Test")
Text("Test")
Text("Test")
Text("Test")
Text("Test")
}
}
}
Any help/insight is very much welcomed,
Thank you