아이폰 프로그램의 헤더 (내비게이션 표시줄) 색상 변경

6038 단어 Swift

요지


이 그림처럼 제작NavigationController하면 변경NavigationBar의 색깔, 제목의 색깔, 시간 표시와 배터리의 색깔을 바꾸는 방법을 소개한다.

절차.


Xcode로 Single View Apps를 제작한 상태부터 시작합니다.
XcodeMain.storyboard가 표시된 상태에서 Editor > Embedin > Navigation Controller 를 선택합니다.

그러면 Navigation Controller가 추가될 것 같습니다.

그런 다음 Navigation Item을 View Controller 화면으로 드래그합니다.이렇게 하면 제목이 표시됩니다.

에서 내비게이션 표시줄의 색을 수정해야 하지만, 색 지정은 원본 코드에 설명됩니다.ViewController.swiftviewDidLoad()방법에서 다음과 같다.
ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController!.navigationBar.barStyle = .black
        self.navigationController!.navigationBar.barTintColor = #colorLiteral(red: 0.1725490196, green: 0.1725490196, blue: 0.1725490196, alpha: 1)
        self.navigationController!.navigationBar.tintColor = .white
        self.navigationController!.navigationBar.titleTextAttributes = [
            .foregroundColor: UIColor.white
        ]
    }
}
보아하니 이렇다.

각 매개 변수는 아래의 그림입니다.tintColor링크와 같이 네비게이션 항목의 색을 바꾸는 매개 변수이다.
색상 지정은 .white, .blue처럼 지정할 수도 있고, #colorLiteral(red: green: blue: alpha:)처럼 지정하면 색상 팔레트를 열어 색상을 자세히 지정할 수도 있습니다.
barStyle에 대해서는 default(아무것도 지정하지 않음), black, blackTrunslucent 세 가지 옵션 중에서 선택할 수 있습니다.default의 상황은 다음과 같다.
blackTrunslucent의 상황은 다음과 같다.black랑 다를 게 없어...스크롤하면 배경이 투명할 줄 알았는데 아닌 것 같아.미안합니다.여기 잘 모르겠어요.

여기까지 읽어주셔서 감사합니다.

참고 자료

  • Change the Color of the Status Bar iOS Tutorial
  • iOS로 머리글을 설정하는 세 가지 방법
  • 탐색 모음 색상 변경
  • UIColor의 정의는 모두 #colorLiteral을 사용했습니다.
  • [iOS] 코드로 내비게이션 표시줄 제목의 글꼴과 크기를 변경합니다[Swift3.0]
  • 좋은 웹페이지 즐겨찾기