【问题标题】:Place floating UIButton on top of UICollectionView将浮动 UIButton 放在 UICollectionView 之上
【发布时间】:2013-05-13 20:49:55
【问题描述】:

我拼命想在我的 UICollectionView 上放置一个 UIButton。目标类似于新的foursquare 应用程序中的签到按钮。

我的代码目前如下所示:

UIButton *floatingButton = [[UIButton alloc] init];
[floatingButton setFrame:CGRectMake(320, 150, 50, 50)];
[floatingButton setBackgroundColor:[UIColor blackColor]];
[self.collectionView addSubview:floatingButton];
[self.collectionView bringSubviewToFront:floatingButton];

问题是按钮不在任何地方。

【问题讨论】:

    标签: ios uibutton uicollectionview


    【解决方案1】:

    您可能希望将按钮添加到 collectionView 的父视图。 最有可能是 self.view,但如果不看更多代码就很难确定。

    而不是

    [self.collectionView addSubview:floatingButton];
    

    你可能想要

    [self.view addSubview:floatingButton];
    

    【讨论】:

    • 但是我也犯了一个愚蠢的错误[floatingButton setFrame:CGRectMake(320, 150, 50, 50)];is outside the viewer 我不得不将它设置为[floatingButton setFrame:CGRectMake(150, 150, 50, 50)];
    • 即使使用这种方法(我认为是正确的)我也无法使按钮可触摸。
    【解决方案2】:

    我遇到了同样的问题。将您的按钮添加到 collectionView superView,可能是 self.view。

    [self.view addSubview:floatingButton];
    

    并将按钮设置为 firstResponder。

    [floatingButton becomeFirstResponder];
    

    【讨论】:

    • superview.addSubview(),而不是 view.addSubview()。至少对于 Swift 4+
    猜你喜欢
    • 2019-02-19
    • 2016-06-04
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多