UINavigationItem стоит за UINavigationBar - PullRequest
0 голосов
/ 16 марта 2020

Я установил UINavigationBar, чтобы закругленные нижние углы были затенены, но он удалил мой UINavigationItem. Я пытался установить его обратно программно, но он устанавливает его позади элемента верхней панели.

    class RoundedShadowCorners {
    func shadowTopBar(_ topBar: UINavigationBar,_ offset: CGFloat,_ navigationItem: UINavigationItem){
        topBar.isTranslucent = false
        topBar.tintColor = UIColor.orange

        topBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        topBar.shadowImage = UIImage()
        topBar.backgroundColor = UIColor.white
        let shadowView = UIView(frame: CGRect(x: 0, y: -offset, width: (topBar.bounds.width), height: (topBar.bounds.height) + offset))
        shadowView.backgroundColor = UIColor.white
        topBar.insertSubview(shadowView, at: 1)

        let shadowLayer = CAShapeLayer()
        shadowLayer.path = UIBezierPath(roundedRect: shadowView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath

        shadowLayer.fillColor = UIColor.white.cgColor

        shadowLayer.shadowColor = UIColor.darkGray.cgColor
        shadowLayer.shadowPath = shadowLayer.path
        shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
        shadowLayer.shadowOpacity = 0.8
        shadowLayer.shadowRadius = 2

        shadowView.layer.insertSublayer(shadowLayer, at: 0)

        topBar.prefersLargeTitles = true
        topBar.topItem?.title = "HJFSKDJKA"
    }
}

смещение равно view.safeAreaInsets.top! Изображение прикреплено, как вы можете видеть, заголовок находится позади слоя.

Текст находится позади

Как видите, слой работает

1 Ответ

0 голосов
/ 16 марта 2020

Я отредактировал код @ Daljeet и благодаря этому я нашел решение:

        let titleLabelView = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        titleLabelView.backgroundColor = UIColor.clear
        titleLabelView.textAlignment = .center
        titleLabelView.textColor = UIColor.black
        titleLabelView.font = UIFont.systemFont(ofSize: 17)
        titleLabelView.adjustsFontSizeToFitWidth = true
        titleLabelView.text = navigationItem.title

        let labelView: UIView = titleLabelView
        topBar.insertSubview(labelView, aboveSubview: shadowView)
        let yCoordPlaceholder = labelView.center.y
        labelView.center = CGPoint(x: safeArea.width/2, y: yCoordNavPlaceholder)

Еще раз спасибо @Daljeet!


Вы можете попробовать код ниже:

 // First create custom label
 UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; 
 [titleLabelView setBackgroundColor:[UIColor clearColor]];
 [titleLabelView setTextAlignment: NSTextAlignmentCenter];
 [titleLabelView setTextColor:[UIColor whiteColor]];
 [titleLabelView setFont:[UIFont systemFontOfSize: 27]]; 
 [titleLabelView setAdjustsFontSizeToFitWidth:YES]; 
 titleLabelView.text = @"You text here";
 // here set in navBar titleView
 topBar.topItem.titleView = titleLabelView;


topBar.bringSubviewToFront(topBar.topItem!.titleView!)

Добавьте эту строку кода после установки заголовка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...