最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - How to prevent navigation title from moving up and down while showing a sheet? - Stack Overflow

programmeradmin6浏览0评论

When I move the sheet up and down, the navigation title does not stay in place but moves up and down as well. How can I make it stay in the initial place ?

class ViewController: UIViewController {
    
    class SheetViewController: UIViewController {
        override func viewDidLoad() {
            self.view.backgroundColor = .systemBlue
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .black
        self.navigationItem.title = "Hello, World!"
        
        let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        navigationController?.navigationBar.titleTextAttributes = textAttributes
    }
    
    @IBAction func openSheet(_ sender: Any) {
        let viewControllerToPresent = SheetViewController()
        if let sheet = viewControllerToPresent.sheetPresentationController {
            sheet.detents = [.large()]
            present(viewControllerToPresent, animated: true, completion: nil)
        }
    }
}

When I move the sheet up and down, the navigation title does not stay in place but moves up and down as well. How can I make it stay in the initial place ?

class ViewController: UIViewController {
    
    class SheetViewController: UIViewController {
        override func viewDidLoad() {
            self.view.backgroundColor = .systemBlue
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .black
        self.navigationItem.title = "Hello, World!"
        
        let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        navigationController?.navigationBar.titleTextAttributes = textAttributes
    }
    
    @IBAction func openSheet(_ sender: Any) {
        let viewControllerToPresent = SheetViewController()
        if let sheet = viewControllerToPresent.sheetPresentationController {
            sheet.detents = [.large()]
            present(viewControllerToPresent, animated: true, completion: nil)
        }
    }
}

Share Improve this question edited Mar 21 at 12:59 vikingosegundo 52.3k14 gold badges139 silver badges183 bronze badges asked Mar 21 at 12:57 Programmer54Programmer54 1879 bronze badges 2
  • Is it desirable that the sheet does not cover the navigation title? Sheets don't do that by default. – Sweeper Commented Mar 21 at 13:04
  • @Sweeper I'd like the navigation title to stay on top and remain visible (never covered by the sheet) – Programmer54 Commented Mar 21 at 13:12
Add a comment  | 

1 Answer 1

Reset to default 1

To prevent the navigation title from moving downwards, you should use a .custom detent that is smaller than the maximum detent height.

sheet.detents = [.custom { context in
    context.maximumDetentValue - 1
}]

If you want the navigation title to be completely visible and not covered by the sheet, you should subtract the height of the navigation bar from the maximum detent height.

sheet.detents = [.custom { [weak self] context in
    context.maximumDetentValue - (self?.navigationController?.navigationBar.frame.height ?? 0)
}]

If the keyboard can be shown when the sheet is displayed, it might push the sheet upwards, and cause the navigation title to move downwards. If that is a concern, you should also take into account the keyboard height, and subtract that as well, in addition to the navigation bar height.

Remember to also invalidateDetents if the navigation bar height or keyboard height changes while the sheet is presented.

发布评论

评论列表(0)

  1. 暂无评论