2017-09-20 38 views
8

Tôi đang làm việc trên một ứng dụng và tôi vừa nâng cấp lên Xcode 9/Swift 4 và cũng nâng cấp iPhone của mình lên iOS 11. Ứng dụng được cài đặt khi tôi cài đặt iOS 11 và tất cả dường như OK cho đến khi tôi chạy nó từ Xcode. Bây giờ tôi đang mắc kẹt với chiều cao NavBar mặc định.iOS 11 - Không thể thay đổi chiều cao Thanh Điều hướng

Code tôi đã sử dụng để thay đổi chiều cao không còn làm việc:

class CustomNavControllerVC: UINavigationController 
{ 
    let navBarHeight : CGFloat = 64.0 
    let navbarBackButtonColor = UIColor(red: 247/255, green: 179/255, blue: 20/255, alpha: 1) 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     print("CustomNavControllerVC > viewDidLoad") 
    } 

    override func viewDidLayoutSubviews() 
    { 
     print("CustomNavControllerVC > viewDidLayoutSubviews") 

     super.viewDidLayoutSubviews() 

     navigationBar.frame.size.height = navBarHeight 
     navigationBar.tintColor = navbarBackButtonColor 
    } 

    override func didReceiveMemoryWarning() 
    { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 


// In my VCs 

override func viewDidLoad() 
{ 
    customizeNavBar() 
} 


func customizeNavBar() 
{ 
    let navbarBackItem = UIBarButtonItem() 
    navbarBackItem.title = "Înapoi" 
    navigationItem.backBarButtonItem = navbarBackItem 

    let navbarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 55, height: 20)) 
    navbarImageView.contentMode = .scaleToFill 

    let navbarLogo = UIImage(named: "NavBarLogo.png") 
    navbarImageView.image = navbarLogo 

    navigationItem.titleView = navbarImageView 
} 

Cho đến nay điều duy nhất tôi có thể tìm về vấn đề này là thế này:

iOS 11 navigation bar height customizing

iOS11 customize navigation bar height

How to correctly set UINavigationBar height in iOS 11

Nhưng tiếc là thông tin được cung cấp không giúp ích gì.

Bất kỳ ý tưởng/đề xuất nào?

+0

Tại sao không thể đi với Chế độ xem tùy chỉnh thay vì 'UINavigationBar'? –

+2

https://openradar.appspot.com/32912789 Có vẻ như nhóm UIKit không bao giờ có ý định hỗ trợ cho 'UINavigationBar' để hỗ trợ chiều cao tùy chỉnh. Nếu bạn muốn mở rộng kích thước của thanh điều hướng, tôi sẽ sử dụng các kỹ thuật mà apple khuyến khích tại đây: https://developer.apple.com/library/content/samplecode/NavBar/Introduction/Intro.html – beyowulf

+0

@mukesh_lokare - Ban đầu là NavBar được cho là có chiều cao chuẩn với hình ảnh logo ở giữa nên tôi quyết định sử dụng NavBarController chuẩn. 30 màn hình và nhiều lớp học sau đó, một người nào đó quyết định nó phải có chiều cao tùy chỉnh nên tôi quyết định sử dụng phương pháp đó. – daydr3am3r

Trả lời

-2

Đã cập nhật 2017.10.6

Tôi cũng gặp vấn đề tương tự. Dưới đây là giải pháp của tôi. Tôi cho rằng kích thước chiều cao là 66.

Giải pháp của tôi đang làm việc tốt iOS 10, 11.

Hãy chọn câu trả lời của tôi nếu nó giúp bạn.

Tạo NavgationBar.swift

import UIKit 

class NavigationBar: UINavigationBar { 

    //set NavigationBar's height 
    var customHeight : CGFloat = 66 

    override func sizeThatFits(_ size: CGSize) -> CGSize { 

     return CGSize(width: UIScreen.main.bounds.width, height: customHeight) 

    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     frame = CGRect(x: frame.origin.x, y: 0, width: frame.size.width, height: customHeight) 

     // title position (statusbar height/2) 
     setTitleVerticalPositionAdjustment(-10, for: UIBarMetrics.default) 

     for subview in self.subviews { 
      var stringFromClass = NSStringFromClass(subview.classForCoder) 
      if stringFromClass.contains("BarBackground") { 
       subview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: customHeight) 
       subview.backgroundColor = .yellow 

      } 

      stringFromClass = NSStringFromClass(subview.classForCoder) 
      if stringFromClass.contains("BarContent") { 

       subview.frame = CGRect(x: subview.frame.origin.x, y: 20, width: subview.frame.width, height: customHeight - 20) 

       subview.backgroundColor = UIColor(red: 20/255, green: 20/255, blue: 20/255, alpha: 0.4) 

      } 
     } 
    } 


} 

Set Kịch bản phân cảnh lớp

enter image description here

Set NavigationBar class

Set Tuỳ chỉnh Navigationbar

Add TestView

enter image description here

Thêm TestView + Set SafeArea

ViewController.swift

import UIKit 

class ViewController: UIViewController { 

    var navbar : UINavigationBar! 

    @IBOutlet weak var testView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //update NavigationBar's frame 
     self.navigationController?.navigationBar.sizeToFit() 
     print("NavigationBar Frame : \(String(describing: self.navigationController!.navigationBar.frame))") 

    } 

    //Hide Statusbar 
    override var prefersStatusBarHidden: Bool { 

     return true 
    } 

    override func viewDidAppear(_ animated: Bool) { 

     super.viewDidAppear(false) 

     //Important! 
     if #available(iOS 11.0, *) { 

      //Default NavigationBar Height is 44. Custom NavigationBar Height is 66. So We should set additionalSafeAreaInsets to 66-44 = 22 
      self.additionalSafeAreaInsets.top = 22 

     } 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

SecondViewController.swift

import UIKit 

class SecondViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 


     // Create BackButton 
     var backButton: UIBarButtonItem! 
     let backImage = imageFromText("Back", font: UIFont.systemFont(ofSize: 16), maxWidth: 1000, color:UIColor.white) 
     backButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.plain, target: self, action: #selector(SecondViewController.back(_:))) 

     self.navigationItem.leftBarButtonItem = backButton 
     self.navigationItem.leftBarButtonItem?.setBackgroundVerticalPositionAdjustment(-10, for: UIBarMetrics.default) 


    } 
    override var prefersStatusBarHidden: Bool { 

     return true 
    } 
    @objc func back(_ sender: UITabBarItem){ 

     self.navigationController?.popViewController(animated: true) 

    } 


    //Helper Function : Get String CGSize 
    func sizeOfAttributeString(_ str: NSAttributedString, maxWidth: CGFloat) -> CGSize { 
     let size = str.boundingRect(with: CGSize(width: maxWidth, height: 1000), options:(NSStringDrawingOptions.usesLineFragmentOrigin), context:nil).size 
     return size 
    } 


    //Helper Function : Convert String to UIImage 
    func imageFromText(_ text:NSString, font:UIFont, maxWidth:CGFloat, color:UIColor) -> UIImage 
    { 
     let paragraph = NSMutableParagraphStyle() 
     paragraph.lineBreakMode = NSLineBreakMode.byWordWrapping 
     paragraph.alignment = .center // potentially this can be an input param too, but i guess in most use cases we want center align 

     let attributedString = NSAttributedString(string: text as String, attributes: [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.paragraphStyle:paragraph]) 

     let size = sizeOfAttributeString(attributedString, maxWidth: maxWidth) 
     UIGraphicsBeginImageContextWithOptions(size, false , 0.0) 
     attributedString.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image! 
    } 




    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



} 

enter image description here enter image description here

Vàng là barbackgroundXem. Opacity màu đen là BarContentView.

Và tôi đã xóa BackgroundColor của BarContentView.

enter image description here

That's It.

+0

Cảm ơn. Tôi sẽ thử. – daydr3am3r

+0

Cảm ơn. Nhưng đầu của khu vực an toàn không được cập nhật. Khu vực an toàn hàng đầu vẫn là 44px. Bạn đã có một giải pháp? Tôi không biết làm thế nào để cập nhật hàng đầu của khu vực an toàn sau khi thiết lập chiều cao thanh điều hướng. –

+0

Tôi đã cập nhật câu trả lời ban đầu. –

Các vấn đề liên quan