我有頂部導航欄一個UIViewController。我想用分段控件替換標題。添加segmentedcontrol到導航欄在一個視圖控制器
class AViewController: UIViewController
{
private var navigationbar = UINavigationBar();
private var segment = UISegmentedControl();
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
buildThebar();
}
func buildThebar()
{
navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 64));
navigationbar.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0);
segment = UISegmentedControl(items: ["Testy", "Tests"]);
segment.sizeToFit();
segment.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0);
let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelbar(sender:)));
cancel.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0);
let title = UINavigationItem(title: "Testing");
title.leftBarButtonItem = cancel;
navigationbar.setItems([title], animated: false);
self.navigationItem.titleView = segment;
// self.navigationItem.leftBarButtonItem = cancel;
self.view.addSubview(navigationbar);
}
什麼問題?代碼對我來說工作正常 –
你能發佈結果的圖片嗎?導航欄是否顯示? –
@cdub:看看我發佈的答案 –