現在、swift 、swiftUIでユーチューブ検索アプリを作成中です。
(TabBano中の一つ)
検索の際、画面下に空白ができてしまう症状が発生しています。
前回、同じような質問をし自己解決で空白は消えたと思ったのですが
(検索→検索後、動画をタップ→動画が表示される)
この最後の画面のみ空白が出てしまいます。
storyboardのunder Opaque Barsにチェックをつけると
検索前画面に余白が消えたので、YouTubeのウェブ表示?に問題があると考えます。
そして表示に関わると思われる2つのファイルについて試しました。
ContentViewswift
1import SwiftUI 2 3 4let vc = UIStoryboard(name: "ContentViewHostingController", bundle: nil).instantiateViewController(identifier: "ContentViewHostingController") 5 6 7 8struct ContentView: View { 9 @State var results:Results? 10 @State private var keyword = "筋トレ" 11 @ObservedObject var searcher = YoutubeSearcher() 12 13 var body: some View { 14 NavigationView{ 15 VStack { 16 HStack { 17 TextField("検索文字列", text: $keyword) 18 .textFieldStyle(RoundedBorderTextFieldStyle()) 19 .padding() 20 21 Button(action: { 22 self.searcher.search(keyword: self.keyword) 23 }) { 24 Text("検索") 25 } 26 .background( 27 Capsule() 28 .foregroundColor(.yellow) 29 .frame(width: 60, height: 30)) 30 .padding(20) 31 32 } 33 34 Spacer() 35 if self.searcher.results != nil { 36 List { 37 ForEach(self.searcher.results!.items) {item in 38 NavigationLink(destination: YoutubeView(url: "https://www.youtube.com/watch?v=" + item.vid.videoId)) { 39 VideoRowView(title: item.snippet.title, imgURL: item.snippet.thumbnails.default.url, description: item.snippet.description) 40 41 } 42 } 43 } 44 } 45 } 46 .navigationBarTitle("検索結果") 47 .navigationBarHidden(true) 48 49 } 50 } 51} 52 53struct ContentView_Previews: PreviewProvider { 54 static var previews: some View { 55 56 ContentView() 57 } 58} 59 60 61class ContentViewHostingController: UIHostingController<ContentView> { 62 63 required init?(coder aDecoder: NSCoder) { 64 super.init(coder: aDecoder, rootView: ContentView()) 65 66 } 67 68 override func viewDidLoad() { 69 super.viewDidLoad() 70 71 } 72}
YoutubeViewswift
1import SwiftUI 2import WebKit 3 4struct YoutubeView: UIViewRepresentable { 5 let url: String 6 7 8 9 func makeUIView(context: Context) -> WKWebView { 10 return WKWebView(frame: .zero) 11 12 } 13 14 func updateUIView(_ uiView: WKWebView, context: Context) { 15 uiView.load(URLRequest(url: URL(string: url)!)) 16 } 17 18 19 20} 21 22struct YoutubeView_Previews: PreviewProvider { 23 static var previews: some View { 24 YoutubeView(url: "https://www.rutles.net") 25 } 26 27}
そして下記のサイトを元に、サイズの変更を試みたのですが
変わらずでした。
https://qiita.com/treastrain/items/31dbe4831f8a0ccdcbe2
そもそも検索後はYouTubeの外部画面になるので
ブラウザ?の問題かと思いWKWebViewについて検索。
参考になりそうな記事↓
https://i-app-tec.com/ios/web-browser.html
を発見しましたが、swiftUIとは別物のようで
よく分からずです・・・
SWiftUIに詳しい方いましたら、アドバイスをお願いしたいです。
よろしくお願い致します。
あなたの回答
tips
プレビュー