ios - 在 iOS 中,TableView 标题内的按钮单击被忽略
<p><p>我正在使用自定义 Header 制作 TableView。在自定义标题中,我有 1 个 ImageView、2 个 UILabel 和 1 个 UIButton。当我在 UIButton 上设置 TouchUpInSide 事件时,它不会触发。</p>
<p>我在 SO 和 google 上看到很多帖子,但没有任何帮助。</p>
<p>这是我尝试过的:</p>
<p>1)。 <a href="https://stackoverflow.com/questions/28486963/swift-custom-tableview-not-executing-click-on-uibutton" rel="noreferrer noopener nofollow">Swift - Custom TableView Not Executing Click on UIButton</a> </p>
<p>2)。 <a href="https://stackoverflow.com/questions/29380732/uibutton-in-uitableview-tableheaderview-not-responding-to-touches" rel="noreferrer noopener nofollow">UIButton in UITableView tableHeaderView not responding to touches</a> </p>
<p>3)。 <a href="https://stackoverflow.com/questions/297297/uibutton-in-a-uitableview-header-ignores-most-touches" rel="noreferrer noopener nofollow">UIButton in a UITableView header ignores most touches</a> </p>
<p>这是我的自定义 header 代码。</p>
<p><strong>代码:</strong> </p>
<pre><code>public override void ViewDidLoad()
{
base.ViewDidLoad();
TableView.Frame = new RectangleF((float)TableView.Frame.Left, 130, (float)TableView.Frame.Width, (float)(View.Frame.Size.Height - 130));
headerView = new UIView();
headerView.Frame = new CoreGraphics.CGRect(0, 0, TableView.Frame.Width, 140);
UIImageView backgroundImage = new UIImageView();
backgroundImage.Image = UIImage.FromFile("drawerbg");
backgroundImage.Frame = new CoreGraphics.CGRect(0, 0, TableView.Frame.Width, 140);
headerView.AddSubview(backgroundImage);
profileImage = new UIImageView();
profileImage.Frame = new CoreGraphics.CGRect(10, 10, 70, 70);
profileImage.Layer.CornerRadius = 35;
profileImage.ClipsToBounds = true;
profileImage.Image = UIImage.FromBundle("default_user");
backgroundImage.AddSubview(profileImage);
userName = new UILabel();
userName.Frame = new CoreGraphics.CGRect(10, 90, TableView.Frame.Width - 20, 20);
userName.Font = GargiFontAndSize.B14();
userName.TextColor = UIColor.White;
backgroundImage.AddSubview(userName);
userOrganization = new UILabel();
userOrganization.Frame = new CoreGraphics.CGRect(10, 110, TableView.Frame.Width - 50, 20);
userOrganization.Font = GargiFontAndSize.B14();
userOrganization.TextColor = UIColor.White;
userOrganization.Text = "Organizaton";
backgroundImage.AddSubview(userOrganization);
organizationArrowButton = new UIButton();
organizationArrowButton.Frame = new CoreGraphics.CGRect(260, 110, 20, 20);
organizationArrowButton.SetImage(UIImage.FromBundle("sort_down"), UIControlState.Normal);
organizationArrowButton.UserInteractionEnabled = true;
organizationArrowButton.ClipsToBounds = false;
backgroundImage.AddSubview(organizationArrowButton);
organizationArrowButton.BringSubviewToFront(headerView);
TableView.SectionHeaderHeight = 0;
TableView.SectionFooterHeight = 0;
var gradient = new CAGradientLayer();
gradient.Frame = headerView.Frame;
gradient.Colors = new CoreGraphics.CGColor[] { GargiColor.PrimaryColor().CGColor, GargiColor.SecondaryColor().CGColor };
headerView.Layer.InsertSublayer(gradient, 0);
TableView.TableHeaderView = headerView;
}
</code></pre>
<p>还有一件事我的 viewController 是扩展 DialogViewController。</p>
<p>任何帮助将不胜感激..</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>UIImageView 的 userInteraction 默认是禁用的,如果你正在向 ImageView 添加按钮,请将 UIImageView 的 userInteractionEnabled 设置为 true。</p></p>
<p style="font-size: 20px;">关于ios - 在 iOS 中,TableView 标题内的按钮单击被忽略,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47712258/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47712258/
</a>
</p>
页:
[1]