programing

이미지를 내비게이션 바 제목으로 넣는 방법

sourcetip 2021. 1. 16. 11:15
반응형

이미지를 내비게이션 바 제목으로 넣는 방법


텍스트로 작성된 제목 대신 내비게이션 바 중앙에 .png 이미지를 넣고 싶습니다. 방법을 알려주시겠습니까?

감사.


navigationItem의 titleView 설정

UIImage *image = [UIImage imageNamed:@"image.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

Swift 4.2
제안 된 프레이밍 / 스케일링 포함

let image: UIImage = UIImage(named: "image.png")!
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
imageView.image = image
self.navigationItem.titleView = imageView

UINavigationBar의 모든보기를 변경할 수 있습니다. 탐색 컨트롤러에서 navigationBar를 변경하려는 경우 다음을 수행하십시오.

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"] autorelease];

navigationBar를 직접 만드는 경우 호출을 self.navigationItem호출하는 대신 동일한 작업을 수행합니다 .navigationBar.topItem


Swift 2.x로 업데이트 됨

navigationItem.titleView = UIImageView.init(image: UIImage(named:"yourImageName"))

누군가 Swift 4.2의 내비게이션 바 제목보기에 이미지와 텍스트가 모두 필요한 경우 다음 기능을 사용해보세요.

override func viewDidLoad() {

    super.viewDidLoad()

    //Sets the navigation title with text and image
    self.navigationItem.titleView = navTitleWithImageAndText(titleText: "Dean Stanley", imageName: "online")
}

func navTitleWithImageAndText(titleText: String, imageName: String) -> UIView {

    // Creates a new UIView
    let titleView = UIView()

    // Creates a new text label
    let label = UILabel()
    label.text = titleText
    label.sizeToFit()
    label.center = titleView.center
    label.textAlignment = NSTextAlignment.center

    // Creates the image view
    let image = UIImageView()
    image.image = UIImage(named: imageName)

    // Maintains the image's aspect ratio:
    let imageAspect = image.image!.size.width / image.image!.size.height

    // Sets the image frame so that it's immediately before the text:
    let imageX = label.frame.origin.x - label.frame.size.height * imageAspect
    let imageY = label.frame.origin.y

    let imageWidth = label.frame.size.height * imageAspect
    let imageHeight = label.frame.size.height

    image.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageHeight)

    image.contentMode = UIView.ContentMode.scaleAspectFit

    // Adds both the label and image view to the titleView
    titleView.addSubview(label)
    titleView.addSubview(image)

    // Sets the titleView frame to fit within the UINavigation Title
    titleView.sizeToFit()

    return titleView

}

enter image description here


Searching for an answer to this question, I've found a nice and simple solution in the Apple Discussions forum, overriding -(void)drawRect:(CGRect)rect for UINavigationBar :

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

Swift 5 version:

navigationItem.titleView = UIImageView(image: UIImage(named: "logo.png"))

ReferenceURL : https://stackoverflow.com/questions/865176/how-to-put-an-image-as-the-navigation-bar-title

반응형