回答

6

您可以使用initWithCustomView:方法在其中創建一個帶有自定義視圖的UIBarButtonItem。例如:

UISwitch *switch = ...; 
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:mySwitch]; 

將此設置爲您的UINavigationItem上的左/右項。

3

這是可能與[UIBarButtonItem initWithCustomView:]

UISwitch* switchView = [[UISwitch alloc] init]; 
UIBarButtonItem* switchItem = [[UIBarButtonItem alloc] initWithCustomView:switchView]; 
self.navigationItem.rightBarButtonItem = switchItem; 
1

這是可能與[UIBarButtonItem initWithCustomView:]

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: mySwitch]; 
1

在Swift-

寫這些代碼的viewDidLoad中()

let switchDemo=UISwitch(frame:CGRectMake(15, 15, 0, 0)) 
     switchDemo.on = true 
     switchDemo.setOn(true, animated: false) 
     switchDemo.addTarget(self, action: "switchValueDidChange:", forControlEvents: .ValueChanged) 
     self.view.addSubview(switchDemo) 

這是你的功能 -

func switchValueDidChange(sender:UISwitch!) 
{ 
if (sender.on == true){ 
println(「on」) 
} 
else{ 
println(「off」) 
} 
} 
相關問題