[英]How to use the UIView.hidden property to change visibility of views
In my IOS application, I need to show a drop-down menu when the menu button is pressed and hide it when the menu button is pressed again. 在我的IOS应用程序中,我需要在按下菜单按钮时显示一个下拉菜单,并在再次按下菜单按钮时隐藏它。 I tried changing the hidden status to false and true as in the code below however that doesn't seem to work.
我尝试将隐藏状态更改为false和true,如下面的代码所示,但这似乎不起作用。
if (menuButtonActive == false)
{
menuButtonActive = true;
DropMenu.Hidden = true;
}
if (menuButtonActive == true)
{
menuButtonActive = false;
DropMenu.Hidden = false;
}
Thanks to anyone that helps! 感谢任何有帮助的人!
It's just simple, try this: 这很简单,请尝试以下操作:
In Swift: 在Swift中:
yourView.isHidden = true //or false
In Objective-C: 在Objective-C中:
yourView.hidden = YES; //or NO;
In C#: 在C#中:
yourView.Hidden = true; //or false;
In your case you are doing it right, but the problem is you are using only if
in both cases. 在你的情况你正在做的是正确的,但问题是你只使用
if
在这两种情况下。 You have to use else if
for second if
condition in order to achieve the desired result. 您必须在第二个
if
条件中使用else if
才能达到所需的结果。
Otherwise the second if
condition will be always true and get executed you will see no effect of first if
block. 否则,第二个
if
条件将始终为true并被执行,您将看不到第一个if
块的影响。
For Your Case: 对于您的情况:
It should be like: 应该是这样的:
menuButtonActive = !menuButtonActive
DropMenu.Hidden = menuButtonActive
Hope this help you! 希望这对您有所帮助! :)
:)
look at follow code , add a else 查看以下代码,添加其他
if (menuButtonActive == false)
{
menuButtonActive = true;
DropMenu.Hidden = true;
}
else if (menuButtonActive == true)
{
menuButtonActive = false;
DropMenu.Hidden = false;
}
It must be simple 一定很简单
menuButtonActive = !menuButtonActive;
DropMenu.Hidden = menuButtonActive;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.