일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Stomp user connection event
- supabase edge function deploy error
- Firebase authentication apple login 최신
- Stomp socket connection event
- Firebase authentication android apple login setting
- 주 4일 근무제 경험담
- Android apple login
- supabase storage feature not enabled for this tenant
- 소개팅앱 만들기
- Supabase edge function insert data
- 소개팅앱 만들기 stomp
- stable-diffusion-webui local 네트워크에서 접속
- no such module '__ObjC'
- supabase storage public url not working
- 소개팅앱 만들기 flutter
- Spring LocalDateTime response array
- Supabase edge function
- Spring boot swagger blank screen
- 주 4일 근무제 log
- Spring google SMTP settings
- Firebase authentication apple login
- Android apple login identifier
- 주 4일 근무제
- DT_TOOLCHAIN_DIR cannot be used
- Firebase authentication android apple login
- invite user for supabase project
- 로컬 서버 접속
- supabase edge function deploy error supabaseurl is required
- stable-diffusion-webui local 접속
- Android apple login 인증서 설정
- Today
- Total
꿀맛코딩
How to enable control of tab bar item in UITabBarController (UITabBarController tab bar item 선택 가능 제어) 본문
How to enable control of tab bar item in UITabBarController (UITabBarController tab bar item 선택 가능 제어)
soycrab 2020. 1. 9. 15:47
UITabBarController를 사용하는데
특정 조건에서 특정 탭이 클릭은 되지만,
페이지가 넘어가지 않고
다른 액션을 실행하고 싶을때가 있다.
이때는 UITabBarControllerDelegate 의
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool 를
이용해 제어가 가능하다.
먼저 MainTabBarController 클래스를 생성하고 아래와 같이 코드를 작성한다.
MainTabBarController.swift
import Foundation
import UIKit
class MainTabBarController: UITabBarController {
var index = 0
override func viewDidLoad() {
self.delegate = self
}
}
extension MainTabBarController: UITabBarControllerDelegate {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if(item.title == "채팅") {
index = 1
} else {
index = 0
}
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return index != 1
}
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) //탭을 클릭하면 클릭한 UITabBarItem 을 가져온다
탭을 클릭하면 클릭하면
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) 로
UITabBarItem이 넘어오고 이를 통해 UITabBarItem 의 title 을 가져올수 있다.
TabBarItem 타이틀을 하나는 홈 하나는 채팅으로 해두었는데,
이걸 비교해서 index 를 가져왔다.
return index != 1 // 선택된 index 가 1 이 아닐 경우에만 선택을 가능하게 한다.
위의 코드는 단순하게 index가 1일 아닐 경우 선택이 불가능하게 했지만
자신이 원하는 조건으로 변경하여 선택 불가능 여부를 변경하면 된다.
index 를 가져오는 방법 또한 여러 가지가 있겠지만 예제 코드 이므로 참고만 하자.
위의 클래스를 작성하였다면 스토리보드로 이동하여
Custom class 의 class 부분에 설정해 준다.
'공부방 > iOS' 카테고리의 다른 글
How to init Pod When Xcode Pod File Error (Pod 파일 에러시 팟 초기화 방법) (0) | 2019.11.14 |
---|---|
XCode ERROR ITMS-90534: Invalid Toolchain (15) | 2019.11.07 |
iOS Fatal Exception: NSInternalInconsistencyException (0) | 2019.11.04 |
iOS 13.2 which may not be supported by this version of Xcode (0) | 2019.10.31 |
How to control href in iOS WKWebView (iOS WKWebView href 조작 하는법) (0) | 2019.10.29 |