【问题标题】:UICollectionView SelectedBackgroundView Width?UICollectionView SelectedBackgroundView 宽度?
【发布时间】:2018-02-25 01:16:13
【问题描述】:

我有 uicollectionview,每个单元格都有一个 SelectedBackgroundView 属性,只有这样的背景颜色:

        SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Green };

如何设置选定背景视图的宽度?我需要它是默认值的一半。

我试过了:

        SelectedBackgroundView.Frame = new CGRect(SelectedBackgroundView.Frame.X, SelectedBackgroundView.Frame.Y, SelectedBackgroundView.Frame.Width / 2, SelectedBackgroundView.Frame.Height / 2);

但这只会扭曲视图

这是我放代码的地方:

    [Export ("initWithFrame:")]
    public PhotoCell(CGRect frame) : base(frame)
    {
        //BackgroundView = new UIView { BackgroundColor = UIColor.Clear  };
        SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Green };
        this.AddConstraint(NSLayoutConstraint.Create(this.SelectedBackgroundView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, this.Bounds.Height / 2));
        imageView = new UIImageView(frame); // set as you want
        ContentView.AddSubview(imageView);
    }

【问题讨论】:

    标签: ios xamarin uicollectionview


    【解决方案1】:

    为什么不添加约束?

    您可以将宽度和高度约束指定为NSLayoutConstraints,然后使用AddConstraint 方法将它们添加到您的UICollectionViewCell。这是F# 中的一个示例:

     myCell.AddConstraint(NSLayoutConstraint.Create(SelectedBackgroundView, NSLayoutAttribute.Height, NSLayoutRelation.Equal,null,NSLayoutAttribute.NoAttribute, 1, myHeightValue))
    

    如果在C# 中不相同,它会非常相似

    【讨论】:

    • 我试过 imageCell.AddConstraint(NSLayoutConstraint.Create(imageCell.SelectedBackgroundView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, imageCell.Bounds.Height/2));它什么也没做
    • 你把这段代码放在哪里?理想情况下,您希望此代码位于 ImageCell 类本身内。请您发布您的完整代码。
    【解决方案2】:

    这段代码解决了我的问题:

            SelectedBackgroundView.Frame = new CGRect(SelectedBackgroundView.Frame.X + 5, SelectedBackgroundView.Frame.Y + 5, SelectedBackgroundView.Frame.Width-10, SelectedBackgroundView.Frame.Height-10);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多