質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

3995閲覧

TabPageViewControllerのエラーを解決させたい

yotubarail

総合スコア23

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/06/13 01:49

編集2019/06/13 02:31

TabPageViewControllerの動きを見てみようとGitHubからダウンロードしSwift5に対応させようとしたのですが、3つほどエラーが残ってしまいました。

そのうちの1つに
Cannot call value of non-function type '((Int, UIPageViewController.NavigationDirection))?'
というものがあり、ひとまずこちらを解決できたらと考えています。

エラー周辺のコードを記載しておきます。

TabPageViewController

1open class TabPageViewController: UIPageViewController { 2 open var isInfinity: Bool = false 3 open var option: TabPageOption = TabPageOption() 4 open var tabItems: [(viewController: UIViewController, title: String)] = [] 5 6 var currentIndex: Int? { 7 guard let viewController = viewControllers?.first else { 8 return nil 9 } 10 return tabItems.map{ $0.viewController }.firstIndex(of: viewController) 11 } 12 fileprivate var beforeIndex: Int = 0 13 fileprivate var tabItemsCount: Int { 14 return tabItems.count 15 } 16 fileprivate var defaultContentOffsetX: CGFloat { 17 return self.view.bounds.width 18 } 19 fileprivate var shouldScrollCurrentBar: Bool = true 20 lazy fileprivate var tabView: TabView = self.configuredTabView() 21 fileprivate var statusView: UIView? 22 fileprivate var statusViewHeightConstraint: NSLayoutConstraint? 23 fileprivate var tabBarTopConstraint: NSLayoutConstraint? 24 25 public init() { 26 super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 27 } 28 29 public required init?(coder aDecoder: NSCoder) { 30 super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 31 } 32 33 override open func viewDidLoad() { 34 super.viewDidLoad() 35 36 setupPageViewController() 37 setupScrollView() 38 updateNavigationBar() 39 } 40 41 override open func viewWillAppear(_ animated: Bool) { 42 super.viewWillAppear(animated) 43 44 if tabView.superview == nil { 45 tabView = configuredTabView() 46 } 47 48 if let currentIndex = currentIndex , isInfinity { 49 tabView.updateCurrentIndex(currentIndex, shouldScroll: true) 50 } 51 } 52 53 override open func viewDidAppear(_ animated: Bool) { 54 super.viewDidAppear(animated) 55 56 updateNavigationBar() 57 tabView.layouted = true 58 } 59 60 override open func viewWillDisappear(_ animated: Bool) { 61 super.viewWillDisappear(animated) 62 63 navigationController?.navigationBar.shadowImage = nil 64 navigationController?.navigationBar.setBackgroundImage(nil, for: .default) 65 } 66} 67 68 69// MARK: - Public Interface 70 71public extension TabPageViewController { 72 73 func displayControllerWithIndex(_ index: Int, direction: UIPageViewController.NavigationDirection, animated: Bool) { 74 75 beforeIndex = index 76 shouldScrollCurrentBar = false 77 let nextViewControllers: [UIViewController] = [tabItems[index].viewController] 78 79 let completion: ((Bool) -> Void) = { [weak self] _ in 80 self?.shouldScrollCurrentBar = true 81 self?.beforeIndex = index 82 } 83 84 setViewControllers( 85 nextViewControllers, 86 direction: direction, 87 animated: animated, 88 completion: completion) 89 90 guard isViewLoaded else { return } 91 tabView.updateCurrentIndex(index, shouldScroll: true) 92 } 93} 94 95 96// MARK: - View 97 98extension TabPageViewController { 99 100 fileprivate func setupPageViewController() { 101 dataSource = self 102 delegate = self 103 automaticallyAdjustsScrollViewInsets = false 104 105 setViewControllers([tabItems[beforeIndex].viewController], 106 direction: .forward, 107 animated: false, 108 completion: nil) 109 } 110 111 fileprivate func setupScrollView() { 112 // Disable PageViewController's ScrollView bounce 113 let scrollView = view.subviews.compactMap { $0 as? UIScrollView }.first 114 scrollView?.scrollsToTop = false 115 scrollView?.delegate = self 116 scrollView?.backgroundColor = option.pageBackgoundColor 117 } 118 119 /** 120 Update NavigationBar 121 */ 122 123 fileprivate func updateNavigationBar() { 124 if let navigationBar = navigationController?.navigationBar { 125 navigationBar.shadowImage = UIImage() 126 navigationBar.setBackgroundImage(option.tabBackgroundImage, for: .default) 127 navigationBar.isTranslucent = option.isTranslucent 128 } 129 } 130 131 fileprivate func configuredTabView() -> TabView { 132 let tabView = TabView(isInfinity: isInfinity, option: option) 133 tabView.translatesAutoresizingMaskIntoConstraints = false 134 135 let height = NSLayoutConstraint(item: tabView, 136 attribute: .height, 137 relatedBy: .equal, 138 toItem: nil, 139 attribute: .height, 140 multiplier: 1.0, 141 constant: option.tabHeight) 142 tabView.addConstraint(height) 143 view.addSubview(tabView) 144 145 let top = NSLayoutConstraint(item: tabView, 146 attribute: .top, 147 relatedBy: .equal, 148 toItem: topLayoutGuide, 149 attribute: .bottom, 150 multiplier:1.0, 151 constant: 0.0) 152 153 let left = NSLayoutConstraint(item: tabView, 154 attribute: .leading, 155 relatedBy: .equal, 156 toItem: view, 157 attribute: .leading, 158 multiplier: 1.0, 159 constant: 0.0) 160 161 let right = NSLayoutConstraint(item: view, 162 attribute: .trailing, 163 relatedBy: .equal, 164 toItem: tabView, 165 attribute: .trailing, 166 multiplier: 1.0, 167 constant: 0.0) 168 169 view.addConstraints([top, left, right]) 170 171 tabView.pageTabItems = tabItems.map({ $0.title}) 172 tabView.updateCurrentIndex(beforeIndex, shouldScroll: true) 173 174 tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: UIPageViewController.NavigationDirection) in // ここでエラーが出る 175 self?.displayControllerWithIndex(index, direction: direction, animated: true) 176 } 177 178 tabBarTopConstraint = top 179 180 return tabView 181 } 182 183 private func setupStatusView() { 184 let statusView = UIView() 185 statusView.backgroundColor = option.tabBackgroundColor 186 statusView.translatesAutoresizingMaskIntoConstraints = false 187 view.addSubview(statusView) 188 189 let top = NSLayoutConstraint(item: statusView, 190 attribute: .top, 191 relatedBy: .equal, 192 toItem: view, 193 attribute: .top, 194 multiplier:1.0, 195 constant: 0.0) 196 197 let left = NSLayoutConstraint(item: statusView, 198 attribute: .leading, 199 relatedBy: .equal, 200 toItem: view, 201 attribute: .leading, 202 multiplier: 1.0, 203 constant: 0.0) 204 205 let right = NSLayoutConstraint(item: view, 206 attribute: .trailing, 207 relatedBy: .equal, 208 toItem: statusView, 209 attribute: .trailing, 210 multiplier: 1.0, 211 constant: 0.0) 212 213 let height = NSLayoutConstraint(item: statusView, 214 attribute: .height, 215 relatedBy: .equal, 216 toItem: nil, 217 attribute: .height, 218 multiplier: 1.0, 219 constant: topLayoutGuide.length) 220 221 view.addConstraints([top, left, right, height]) 222 223 statusViewHeightConstraint = height 224 self.statusView = statusView 225 } 226 227 228} 229

TabView

1internal class TabView: UIView { 2 3 var pageItemPressedBlock: ((Int, UIPageViewController.NavigationDirection))?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takabosoft

2019/06/13 02:10

どの行でエラー出てるんでしょうか?
yotubarail

2019/06/13 02:13

fileprivate func configuredTabView() -> TabView の中の tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: UIPageViewController.NavigationDirection) in の部分に出ています。 メモをつけたのですが、わかりづらかったですね。申し訳ありません。
takabosoft

2019/06/13 02:17

ああ、横スクロールしたら載っていました、失礼しました。
takabosoft

2019/06/13 02:27

TabViewのpageItemPressedBlockの定義も載せてください。
fuzzball

2019/06/13 02:37

4.2に対応している(らしい)ブランチがありますが、それを元にしているのでしょうか?
guest

回答1

0

ベストアンサー

var pageItemPressedBlock: ((Int, UIPageViewController.NavigationDirection))?

var pageItemPressedBlock: ((_ index: Int, _ direction: UIPageViewController.NavigationDirection) -> Void)?

かなと。

投稿2019/06/13 02:34

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yotubarail

2019/06/13 02:46

ありがとうございます。 解決しました。 またfuzzballさんのご指摘の通り、4.2に対応したブランチを見落としていました。 他の方から引き継いだため、元のものをきちんと確認していなかったのが問題でした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問