iOS : View visible but not active
<p><p>我有一个 <code>UIViewController</code> 来初始化一个 <code>UIView</code>。
此 View 包含交互式元素,如 <code>UITextField</code> 或 <code>UIButton</code>。
View 被添加到 ViewDidLoad 上,位于方法的底部,以确保当我让它可见时,用户交互可以访问它。</p>
<p>但是当我显示 View 时,无法在此 View 上进行任何交互。</p>
<p>这是唯一可能的吗?我做错了吗?</p>
<pre><code>The View
public class AddBusinessEventView : UIView
{
public UILabel LblTitle;
public UITextField TxtType;
public UIButton BtnClose;
public AddBusinessEventView(nfloat bw, nfloat bh)
{
//Bouton pour fermer le popup
BtnClose = new UIButton();
BtnClose.SetImage(UIImage.FromFile("Images/Boutons/lightbox_close.png"), UIControlState.Normal);
BtnClose.Frame = new CGRect(bw - 80, 30, BtnFermer.ImageView.Image.CGImage.Width * 0.5, BtnFermer.ImageView.Image.CGImage.Height * 0.5);
//Doit se trouver par dessus txtSite et ajouté après dans la vue pour se trouvé en premier plan
LblTitle = new UILabel();
LblTitle.Frame = new CGRect((bw - (lw + 200)) / 2, 100, lw + 200, 30);
LblTitle.Text = "Fill with your event elements";
LblTitle.Font = UIFont.FromName("GillSans-Bold", 22);
LblTitle.TextColor = UIColor.FromRGB(211, 3, 67);
LblTitle.TextAlignment = UITextAlignment.Center;
TxtType = new UITextField(new CGRect((bw - 750) / 2, 140, 350, 40));
TxtType.BackgroundColor = UIColor.White;
TxtType.TextAlignment = UITextAlignment.Center;
TxtType.BorderStyle = UITextBorderStyle.RoundedRect;
TxtType.AutocorrectionType = UITextAutocorrectionType.No;
TxtType.AutocapitalizationType = UITextAutocapitalizationType.AllCharacters;
TxtType.Placeholder = "Type";
AddSubviews(BtnClose, LblTitle, TxtType);
}
}
</code></pre>
<p>UIViewController</p>
<pre><code>partial class EvenementViewController : EnhancedUIViewController
{
AddBusinessEventView AddBusinessEventView;
public EvenementViewController(IntPtr handle) : base(handle) { }
public EvenementViewController() : base() { }
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
if (myEvent == null)
{
ShowAddBusinessEventView();
}
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
nfloat bw = View.Bounds.Width;
nfloat bh = View.Bounds.Height;
//Another Elements are adding to view here
//...
AddBusinessEventView = new AddBusinessEventView(bw, bh);
AddBusinessEventView.Hidden = true;
//Much more View.Add with all elements here
//...
View.Add(AddBusinessEventView);
AddBusinessEventView.BtnType.TouchUpInside += BtnClose_TouchUpInside;
}
#region BusinessEventAdd
void ShowAddBusinessEventView()
{
UIView.Animate(duration: 1,
delay: 0,
options: UIViewAnimationOptions.CurveEaseInOut,
animation: () =>
{
AddBusinessEventView.Alpha = 1.0f;
},
completion: () =>
{
AddBusinessEventView.Hidden = false;
AddBusinessEventListener();
}
);
}
void HideAddBusinessEventView()
{
UIView.Animate(duration: 1,
delay: 0,
options: UIViewAnimationOptions.CurveEaseInOut,
animation: () =>
{
AddBusinessEventView.Alpha = 0.0f;
},
completion: () =>
{
AddBusinessEventView.Hidden = true;
RemoveBusinessEventListener();
}
);
}
void BtnClose_TouchUpInside(object sender, EventArgs e)
{
System.Diagnostics.Debug.Print("Touching myself");
}
#endregion
}
</code></pre>
<p>请将 EnhancedViewController 视为标准 UIViewController,我只是添加一些增强功能以向用户显示来自 Overlay 的消息。</p>
<p>正如我所说,我们既不能与 TxtType 也不能与 BtnClose 交互。</p>
<p>编辑:
不确定它是否有帮助;但是当 <code>View</code> 添加到 <code>UIViewController</code> 上的 Main <code>View</code> 时,它显示得很好,但是所有用户交互都在这个 <code>查看</code>
即: <code>AddBusinessEventView</code> 充当弹出窗口,因此它覆盖了所有其他元素,当我按下一个元素时,如果在此 <code>View</code> 之前添加另一个元素,它就是这个元素而不是捕获触摸事件的 <code>AddBusinessEventView</code> 元素。</p>
<p>如前所述,主要目的是在不同文件上剪切 View 元素,以提高该应用程序的可读性和可维护性。<code>在此处输入代码</code></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你没有理由不能这样做。
您可以像 </p> 那样构建您的代码
<ul>
<li>UIViewController
<ul>
<li> Controller 1</li>
<li> Controller 2</li>
<li>...</li>
</ul></li>
<li>观看次数
<ul>
<li>查看1</li>
<li>查看2</li>
<li>...</li>
</ul></li>
</ul>
<p>然后在任意一个Controller中使用View1和View2。</p>
<p>我尝试了您的代码,并且正确弹出了所有内容。
如您所说,如果 View 显示在屏幕上,但它是来自下方与用户交互的另一个 View 的元素,也许您可以尝试将您的 View 置于前面。</p>
<p>试试这个</p>
<pre><code>View.BringSubviewToFront(AddBusinessEventView);
</code></pre>
<p>在 ViewDidLoad 函数结束时。</p></p>
<p style="font-size: 20px;">关于iOS : View visible but not active,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44065320/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44065320/
</a>
</p>
页:
[1]