나는 만들고있는 앱에 테마 추가를 구현하고 싶습니다. 단순히 2 가지 색상 사이를 전환하기 만하면 네비게이션 바, 툴바 등을 선택한 색상으로 표시하고 싶습니다.UIAppencence 코드 분리하기
앱을 처음로드 할 때 AppDelegate의 didFinishLaunchingWithOptions
메소드에서 한 가지 색상을 적용합니다.
UIColor *blueTheme = [UIColor colorWithRed:80/255.0f green:192/255.0f blue:224/255.0f alpha:1.0f];
UIColor *pinkTheme = [UIColor colorWithRed:225/255.0f green:87/255.0f blue:150/255.0f alpha:1.0f];
[[UINavigationBar appearance] setBarTintColor:pinkTheme];
[[UIToolbar appearance] setBarTintColor:pinkTheme];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
첫 번째보기 컨트롤러에서는 색을 전환 할 분할 컨트롤을 넣었습니다.
- (IBAction)themeChosen:(UISegmentedControl *)sender
{
if (sender.selectedSegmentIndex == 0) {
UIColor *blueTheme = [UIColor colorWithRed:80/255.0f green:192/255.0f blue:224/255.0f alpha:1.0f];
[[UINavigationBar appearance] setBarTintColor:blueTheme];
[[UIToolbar appearance] setBarTintColor:blueTheme];
} else {
UIColor *pinkTheme = [UIColor colorWithRed:225/255.0f green:87/255.0f blue:150/255.0f alpha:1.0f];
[[UINavigationBar appearance] setBarTintColor:pinkTheme];
[[UIToolbar appearance] setBarTintColor:pinkTheme];
}
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
기본 테마는 핑크색입니다. 분할 된 컨트롤에서 파란색으로 전환하고 UIToolBar
이있는 다음보기 컨트롤러로 이동합니다. 새로 선택한 색상 (파란색)은 UIToolBar
에만 적용되고 UINavigationBar
에는 적용되지 않습니다.
더 좋은 방법이 있나요? 또한 많은 코드를 반복하기 때문에 테마와 관련된 코드를 별도의 클래스에 넣고 싶습니다. 어떻게해야합니까?
감사합니다.
지금은 이해. 감사 :) – Isuru