- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试快速构建一个社交媒体应用程序,它有一个标签栏,其中有 4 个图标,每个图标都连接到一个 View Controller 。我想让四个 Collection View (每个都与一个 View Controller 相关联)在按下相应的选项卡栏图标时滚动回顶部(就像推特一样)。
只有当过去的标签栏图标用于相应的 Collection View 时,我才希望 Collection View 返回顶部的逻辑(这样它不会在单击时自动滚动到顶部在各自的选项卡上,但需要再次按下)我试图调试我的第一个开关案例,但没有运气,所以我还没有其他案例的完整代码。我尝试做的是引用正确的 Storyboard,然后是正确的 View Controller (在本例中为 homeViewController
),最后调用相应的 Collection View 并使用 setContentOffset
来实现它到顶部。
下面我提供了我的整个 tabBarController
类。
import Foundation
import UIKit
class MainTabController: UITabBarController, UITabBarControllerDelegate {
var pastTabBar: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let tabBarIndex = tabBar.items?.index(of: item)
if tabBarIndex == pastTabBar {
switch tabBarIndex {
case 0:
print ("go to top of home")
//updateHomeCollection = 1
let HomeSB : UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
let HomeVC = HomeSB.instantiateViewController(withIdentifier: "Home") as? HomeViewController
HomeVC?.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
case 1:
print ("go to top of search")
case 2:
print ("go to top of notifications")
case 3:
print ("go to top of profile")
default:
print ("not working")
}
}
if let tabBarIndex = tabBarIndex {
pastTabBar = tabBarIndex
}
}
}
当我点击主页选项卡时,它一直在中止信号!它正在打印这个确切的声明:
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
在这行代码中:
HomeVC?.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
最佳答案
加载 ViewController
不是最好的方法。您必须从 Navigation
中获取所需的 ViewController
,如下所示
extension HomeTabVC: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if previousController == viewController {
if let nav = viewController as? UINavigationController, let vc = nav.viewControllers[0] as? FeaturedVC {
if vc.isViewLoaded && (vc.view.window != nil) {
let visibleIndex = vc.collectionFeatured.indexPathsForVisibleItems
if visibleIndex.count != 0 {
vc.collectionFeatured.scrollToItem(at: IndexPath (item: 0, section: 0), at: .bottom, animated: true)
}
}
}else if let nav = viewController as? UINavigationController, let vc = nav.viewControllers[0] as? CategoryVC {
if vc.isViewLoaded && (vc.view.window != nil) {
let visibleIndex = vc.collectionViewCategory.indexPathsForVisibleItems
if visibleIndex.count != 0 {
vc.collectionViewCategory.scrollToItem(at: IndexPath (item: 0, section: 0), at: .bottom, animated: true)
}
}
}else{
}
}
previousController = viewController
}
}
您必须将 Selected ViewController
存储在变量中,这样当您将 ViewController
从一个选项卡更改为另一个选项卡时,它只会更改 ViewController
并且当您再次点击同一个标签,它会让你滚动回到顶部。这就是 iOS 在所有应用程序中 self 完成的方式
关于ios - 当按下相应 View Controller 的 tabBar 图标时,如何将 collectionView 带回顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607180/
我想将照片数据从 Tabbarcontroller 中的一个 Viewcontroller(cameraVC) 传递到同一 Tabbarcontroller 中的另一个 Viewcontroller(
我需要加载选项卡栏项目。在这里,我需要不同标签中标签栏的不同背景颜色。我正在更改 didSelectItem 中的栏色调颜色。但它的背景颜色没有改变。加载标签栏时它工作正常。 这是我的代码 over
我想获取标签栏默认大小,但我在ViewController中没有任何tabBar,所以我没有使用self.tabBarController?.tabBar.frame.size.height . 我使
我有一个应用程序的想法,但在使用 Xcode 几个月后,我遇到了一个主要的设计问题。我想要这个: 但正如许多 ios 开发人员所知,在界面构建器中使用传统的标签栏只会创建单色、非轮廓、标签栏按钮,如下
我在我的应用程序上使用 TabBarController,在其中一个主视图 (aViewController) 中,我按下另一个 Controller (bViewController),并使用 se
我想在用户点击选项卡时切换选定的选项卡。假设我有两个选项卡并且显示第一个选项卡,那么点击第一个选项卡(以及点击第二个选项卡)应该会引导我进入第二个选项卡。我已经实现了一个自定义 UITabBarCon
我有一个带有 3 个选项卡的选项卡栏 Controller ,每个选项卡都有导航 Controller ,在每个导航 Controller 的 Root View Controller 上我想要选项卡
我正在编写一个 iPhone 应用程序,它类似于 5-6 级向下钻取应用程序。我几乎完成了我的应用程序的编写,现在提出了一个新要求,即从第三个屏幕开始添加一个 TabBar(JQuery 中的导航栏)
我正在使用 SWRevealViewController 作为侧边菜单。我的应用程序中还有一个标签栏 Controller 作为主视图。 在我的菜单中,我有“MenuItem”(显示MenuItemV
我正在通过 Storyboard 创建一个 TabBar Controller 应用程序。我正在使用 Storyboard 引用对象来链接第三个选项卡项的另一个 Storyboard 。它工作正常,但
TabBar 没有隐藏在推送的 ViewController 中,我使用下面的代码来隐藏 tabBar, tabBarController?.tabBar.isHidden = true
我目前有两个 View Controller ,一个是使用 imagePicker 拍照的 CameraViewController,另一个是显示一个人收到的所有照片消息的 PhotoInboxVie
我有一个包含 5 个项目的 UITabBarController。在其中一个 ViewController 上,我有一个按钮,我将其称为“开始”,所以当我按下开始时,我想插入另一个 ViewContr
我正在更改 setSelectionIndicatorImage,当我在 iOS 8 上运行应用程序时,我得到图像和 tabBar 的常规宽度之间的间距。有没有一种方法可以使标签栏的高度与 setSe
我想通过在图像上向左滑动从“正在显示”导航到“即将推出”,此外,我希望 Appbar 在我滑动时不要移动,但我认为只有标签栏和我不确定,如果您知道如何实现这一点,请提供一些建议 enter image
在我的项目中,我有一个 UITabBarController。在其中一个 ViewControllers 上我有一个按钮。当我单击此按钮时,一个新的 ViewController 将以模态方式呈现。
我是 iPhone 开发新手,多个 View (xib 或 nib)真的让我感到困惑。这就是我想要实现的目标...... 使用 TabBarControllerAppDelegate 有 5 个不同的
滚动到底部后如何修复 iOS 15 标签栏透明: 最佳答案 在 iOS 15 中,Apple 添加了 scrollEdgeAppearance用于在边缘滚动时配置标签栏外观的属性。 https://d
我尝试创建如下图所示的自定义标签栏: 下面是我得到的结果: 以下是我当前的代码: class CustomTabBarController: UITabBarController { over
Widget build(BuildContext context) { return Scaffold( body: SingleChildScrollView( child: Colu
我是一名优秀的程序员,十分优秀!