回答

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”) 
} 
} 
相关问题