我的iOS应用程序使用标签栏控制器,当用户点击标签栏中的“记录”图标uitabbaritem时,我想要更改图标图像并立即开始录制音频,然后当新的“记录”时图像被点击,我希望图像恢复到原始图像。我无法实现图像切换和启动录音,因为我不知道如何正确访问uitabbaritem或tabbarcontroller。
在Swift中,如何访问uitabbaritem以便执行这些操作?
答案 0 :(得分:0)
您可以按照以下方式将图像指定给标签栏。
let myTab = self.tabBarController!.viewControllers?[0].tabBarItem as UITabBarItem! //get the desire tab by passing index as 0,1,2,3... Currently i am pointing to first tab.
myTab.image = UIImage(named: "image1")//This image will appear when tab is not selected
myTab.selectedImage = UIImage(named: "image2")//This image will appear when tab is selected
现在要检查是否正在点击相同的标签,请实施委托方法,
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if viewController.tabBarItem.tag == 0 { // assuming this is your desired so called record tab
if !self.isRecordSelected {// create a property isRecordSelected of Bool type
tabBarItem.selectedImage = UIImage(named: "new record image")//This image will appear when tab is recording starts.
self.isRecordSelected = true
} else {
tabBarItem.selectedImage = UIImage(named: " revert image")//This image will appear when tab is recording stops.
self.isRecordSelected = false
}
}
}
为每个标签指定标签号,以便识别。在你的类中设置UITabBarDelegate委托。
答案 1 :(得分:0)
据推测,您希望在录制时显示某种类型的UI,对吗?您可以简单地允许标签栏项执行其常用功能并切换到/加载视图控制器。在视图控制器中,立即启动录制。