버튼을 눌렀을 때 효과가 없으니까 사용자가 아리까리할 수도 있겠다 싶어 요놈자식을 손봐야지 싶었다.
관련 UIButton 메소드가 없어서 따로 설정을 해야 하는 것이 번거롭다.
그래서 CustomButton 클래스를 하나 새롭게 만들어버렸다.
근데 만들었으나 만든 것이 유용하지 않다고 느끼는 중이라 리뷰를 기다리는 중
class CustomButton: UIButton {
// 이 부분이 pressed 시에 나타나는 효과를 설정한 부분이다.
**override var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? .lineGray : .mainBlue
}
}**
override init(frame: CGRect) {
super.init(frame: frame)
configUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
**private** func configUI() {
self.isUserInteractionEnabled = false
self.layer.cornerRadius = 4
self.setTitleColor(.white, for: .normal)
self.backgroundColor = .lineGray
self.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
}
}
Highlight a button when Pressed In swift
스토리보드로 쇽샥하는 건 참 쉬웠는데 코드로 하면 익숙하지 않은 것을 해주려면 매순간이 구글링의 연속이다. 윽
탭바에서 title, image, selectedImage 총 3개의 요소가 필요한데 매번 반복되길래
setNavigationController
메소드에 필요한 파라미터를 설정해줬다.
class MainTabVC: UITabBarController {
//MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
configUI()
configVC()
}
//MARK: - Custom Method
private func configUI() {
view.backgroundColor = .white
}
private func configVC() {
let home = setNavigationController(title: "홈",
unselectedImage: Const.Image.home!,
selectedImage: Const.Image.homeFill!,
rootViewController: HomeVC())
let shorts = setNavigationController(title: "Shorts",
unselectedImage: #imageLiteral(resourceName: "shortsIcon"),
selectedImage: #imageLiteral(resourceName: "shortsIconFill"),
rootViewController: ShortsVC())
let add = setNavigationController(title: "추가",
unselectedImage: #imageLiteral(resourceName: "plueCircleIcon"),
selectedImage: #imageLiteral(resourceName: "plueCircleIcon"),
rootViewController: AddVC())
let subscribe = setNavigationController(title: "구독",
unselectedImage: #imageLiteral(resourceName: "subscriptionsIcon"),
selectedImage: #imageLiteral(resourceName: "subscriptionsIconFill"),
rootViewController: SubscribeVC())
let library = setNavigationController(title: "보관함",
unselectedImage: #imageLiteral(resourceName: "LibraryIcon"),
selectedImage: #imageLiteral(resourceName: "LibraryIconFill"),
rootViewController: LibraryVC())
viewControllers = [home, shorts, add, subscribe, library]
tabBar.tintColor = .black
tabBar.backgroundColor = .white
}
private func **setNavigationController**(title: String,
unselectedImage: UIImage,
selectedImage: UIImage,
rootViewController: UIViewController) -> UINavigationController {
let nav = UINavigationController(rootViewController: rootViewController)
nav.tabBarItem.title = title
nav.tabBarItem.image = unselectedImage
nav.tabBarItem.selectedImage = selectedImage
nav.navigationBar.isHidden = true
return nav
}
}