포스트

Todoey (11)

UINavigation Bar 수정하기

1. Navigation Bar Title 크기 키우기

Navigation Bar의 Title 이 좀 작다고 느껴진다면 이 부분을 체크를 하면 좀 더 크게 볼 수 있다. CleanShot 2024-04-16 at 15 20 05@2x

before simulator_screenshot_2FE2FEE5-C70A-44C0-AA7C-A50608F1F53B

after simulator_screenshot_4ADE9517-74CC-4AC2-A3BF-3A51F7C17B47

Title이 훨씬 더 커졌다.

2. Navigation Bar Color 변경 하기.

Category의 색에 맞게 Nav Bar의 색상을 변경하려고 한다.

1
2
3
4
5
6
7
// Viewdidload
if let colorHex = selectedCategory?.color {
            
            guard let navBar = navigationController?.navigationBar else { fatalError("Navigation Controller does not exist.")}
            
            navigationController?.navigationBar.barTintColor = UIColor(hexString: colorHex)
        }

이렇게 작성을 해준다.

실행하니 에러가 발생한다.

1
Fatal error: Navigation Controller does not exist.

존재하지 않는다는것.

그렇다면 이유는 하나.

NavigationController의 속성이 업데이트 되기 전에 ViewDidLoad가 호출된다.

CleanShot 2024-04-16 at 15 28 48@2x

에러가 발생한 시점에서 NavigationController에 대한 내용은 아무것도 없었다.

이렇땐 VC의 생명주기를 고려하여 호출을 해야한다.

1
2
3
4
5
6
7
8
 override func viewWillAppear(_ animated: Bool) {
        if let colorHex = selectedCategory?.color {
            
            guard let navBar = navigationController?.navigationBar else { fatalError("Navigation Controller does not exist.")}
            
            navigationController?.navigationBar.barTintColor = UIColor(hexString: colorHex)
        }
    }

ViewWillAppear를 사용해주었다.

3. 기타 디자인

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
32
33
34
// CategoryVC
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = super.tableView(tableView, cellForRowAt: indexPath)
        
        if let category = categories?[indexPath.row] {
            cell.selectionStyle = .none
            
            guard let categoryColor = UIColor(hexString: category.color) else {fatalError()}
            cell.textLabel?.text = categories?[indexPath.row].name ?? "No Categories added Yet"
            cell.backgroundColor = categoryColor
            cell.textLabel?.textColor = ContrastColorOf(categoryColor, returnFlat: true)
        }
        return cell
    }

// TodoListVC
override func viewWillAppear(_ animated: Bool) {
        if let colorHex = selectedCategory?.color {
            
            title = selectedCategory?.name
            
            guard let navBar = navigationController?.navigationBar else { fatalError("Navigation Controller does not exist.")}
            
            if let navBarColor = UIColor(hexString: colorHex) {
                navBar.barTintColor = navBarColor
                navBar.tintColor = ContrastColorOf(navBarColor, returnFlat: true)
                navBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor:
                ContrastColorOf(navBarColor, returnFlat: true)]
                searchBar.barTintColor = navBarColor
            }
         
        }
    }    

버전의 문제도 일부 있을듯한데 NavBar에대한 색이 변경이 되지 않았다. 해당문제는 나중에 다시 해결해보는걸로.

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.