3

我正在使用这种方法设置titleView

func addConstrainstToSearchField(with size: CGSize? = nil) {
        var width : CGFloat = 0.0
        if let searchField = self.searchfield {
            var frame = searchField.frame
            if size != nil, let frameWidth = size?.width {
                width = frameWidth
            } else if let frameWidth = self.navigationController?.navigationBar.frame.size.width {
                width = frameWidth
            }
            frame.size.width = (width - 145.0) * 0.90 // 136 is width of (left and right BarButtonItems) button
            searchField.frame = frame
            self.navigationItem.titleView = searchField
//            DispatchQueue.main.async(execute: {
//                self.navigationItem.titleViewWithConstraint = searchField
//            })
        }
    }

我从 viewdidload 和 viewWillTransiion 调用这个方法

但它没有正确显示

屏截图

图 2

提前致谢

4

0 回答 0