我希望在UINavigationItem的右上角有一个UISwitch,而不是UIBarButton。这是否可以在不创建UINavigationItem子类化的情况下实现?
发布于 2013-04-12 18:32:12
您可以使用initWithCustomView:方法创建一个UIBarButtonItem,其中包含一个自定义视图。例如:
UISwitch *switch = ...;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:mySwitch];将其设置为UINavigationItem上的左/右项目。
发布于 2013-04-12 18:32:29
使用[UIBarButtonItem initWithCustomView:]是可能的
UISwitch* switchView = [[UISwitch alloc] init];
UIBarButtonItem* switchItem = [[UIBarButtonItem alloc] initWithCustomView:switchView];
self.navigationItem.rightBarButtonItem = switchItem;发布于 2016-05-25 15:45:49
在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”)
}
}https://stackoverflow.com/questions/15968953
复制相似问题