Подтвердить что ты не робот

Как поместить изображение в качестве заголовка панели навигации

Я хочу поместить изображение .png в середине панели навигации вместо заголовка, написанного в тексте. Не могли бы вы дать мне знать, как это сделать?

Спасибо.

4b9b3361

Ответ 1

Установите заголовок navigationItem titleView

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

Ответ 2

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

Ответ 3

Вы можете изменить все виды в UINavigationBar. Если вы пытаетесь изменить навигацию в навигационном контроллере, выполните следующие действия:

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

Если вы создаете navigationBar, сделайте то же самое, но вместо вызова self.navigationItem: navigationBar.topItem

Ответ 4

Обновлено с помощью Swift 2.x

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

Ответ 5

В случае, если кому-то нужны изображения и текст в представлении заголовка панели навигации в 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 aspect ratio:
    let imageAspect = image.image!.size.width / image.image!.size.height

    // Sets the image frame so that it 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

Ответ 6

В поисках ответа на этот вопрос я нашел хорошее и простое решение в форуме Apple обсуждения, переопределяя -(void)drawRect:(CGRect)rect для 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

Ответ 7

Версия Swift 5:

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