I want to have a UISwitch in the right corner of a UINavigationItem, instead of a UIBarButton. Is this possible without subclassing the UINavigationItem?
问问题
2364 次
6 回答
7
UIBarButtonItem
您可以使用该initWithCustomView:
方法在其中创建一个带有自定义视图的视图。例如:
UISwitch *switch = ...;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:mySwitch];
将此设置为UINavigationItem
.
于 2013-04-12T10:32:11.580 回答
3
有可能[UIBarButtonItem initWithCustomView:]
:
UISwitch* switchView = [[UISwitch alloc] init];
UIBarButtonItem* switchItem = [[UIBarButtonItem alloc] initWithCustomView:switchView];
self.navigationItem.rightBarButtonItem = switchItem;
于 2013-04-12T10:32:29.487 回答
2
在斯威夫特-
在 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”)
}
}
于 2016-05-25T07:45:48.913 回答
1
有可能[UIBarButtonItem initWithCustomView:]
:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: mySwitch];
于 2013-04-12T10:34:12.870 回答
0
对于迅速 4:
@IBOutlet weak var hostGuestSwitchOutletDeclare: UISwitch!
private func loadHostGuestSwitch(){
hostGuestSwitchOutletDeclare.isOn = true
hostGuestSwitchOutletDeclare.setOn(true, animated: false)
hostGuestSwitchOutletDeclare.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged)
if self.hostGuestSwitchOutletDeclare.isOn {
self.navigationItem.title = "ON"
} else {
self.navigationItem.title = "OFF"
}
}
@objc func switchValueDidChange(sender:UISwitch!) {
if (sender.isOn == true){
self.navigationItem.title = "ON"
} else {
self.navigationItem.title = "OF"
}
}
于 2019-04-20T09:15:57.503 回答
0
斯威夫特版本
let customSwitch = UIBarButtonItem(customView: UISwitch())
navigationItem.rightBarButtonItem = customSwitch
在 viewDidLoad 中执行此操作
于 2021-01-23T19:50:26.473 回答