ios - 如何制作 View 滚动?就像 Instagram 的个人资料一样

标签 ios swift uiview uiscrollview

ScrollView 或类似的东西中的 UIView、SegmentedController 和 UIContainerView?

在我的 Storyboard中,我有一个 VC,它在顶部包含一个 UIView,在中间包含分段 Controller ,在底部包含 2 个 ContainerViewController,并嵌入了到新 VC 的 segue。

在新的 VC 中,我有一个 TableView 和一个 CollectionView(例如,Instagram 个人资料)。

我的问题是我需要 SegmentedController 和 UIView 与 ContainerView(TableView/CollectionView) 一起滚动,目前只有 ContainerView 部分滚动,上面的部分是固定的。

现在我猜我可能需要将所有内容都放入 UIScrollView 中,所以我尝试了并正确放置了所有的约束。但是当涉及到在 Swift 文件中配置它时,我现在才真正了解如何设置滚动条的高度,但很明显,我需要的高度可以变化,而不是固定高度!

如果有人能在这里帮助我,那就太好了,或者可能会指出我在这里已经提出的类似问题?我已经看过了,但不幸的是没有找到任何东西!

下面是一张图片,基本上解释了我所追求的,如果上面我不是很清楚..

这是您可以看到的示例:https://github.com/Jackksun/ScrollIssue

enter image description here

最佳答案

据我了解,您想让 ScrollView 接收超出其范围的触摸。您可以通过覆盖实际接收触摸的 View 上的 hitTest:withEvent: 方法来实现此目的。

这是我在我的项目中所做的一个例子。我将 UIView 子类化并将它接收到的所有触摸重定向到特定 View 。在您的情况下,您会将所有触摸重定向到 ScrollView 。

.h file:

@import UIKit.UIView;

/*!
 @class         TouchableView
 @abstract      Touchable view.
 */
@interface TouchableView : UIView

/*!
 @property      viewToReceiveTouches
 @abstract      View that receives touches instead of a given view.
 */
@property (nonatomic, weak) IBOutlet UIView *viewToReceiveTouches;

@end
.m file:

@import UIKit.UIButton;
@import UIKit.UITableView;
@import UIKit.UITextField;

#import "TouchableView.h"

@implementation TouchableView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if ( [self shouldViewReceiveTouch:self inPoint:point] )
    {
        return [super hitTest:point withEvent:event];
    }
    else if ( CGRectContainsPoint(self.bounds, point) && self.isUserInteractionEnabled )
    {
        return self.viewToReceiveTouches;
    }

    return nil;
}

- (BOOL)shouldViewReceiveTouch:(UIView *)view inPoint:(CGPoint)point
{
    if ( !CGRectContainsPoint(view.bounds, point) || !view.isUserInteractionEnabled )
    {
        return NO;
    }

    if ( [view isKindOfClass:[UIButton class]] ||
         [view isKindOfClass:[UITextField class]] ||
         [view isKindOfClass:[UITableViewCell class]] ||
         [view isKindOfClass:[UITableView class]] )

    {
        return YES;
    }

    for ( UIView *subview in view.subviews )
    {
        if ( [self shouldViewReceiveTouch:subview inPoint:[view convertPoint:point toView:subview]] )
        {
            return YES;
        }
    }

    return NO;
}

@end

关于ios - 如何制作 View 滚动?就像 Instagram 的个人资料一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35011539/

相关文章:

ios - 在服务器上检查标志的最有效方法

ios - 将 UIView 子类链接到 xib 文件

ios - 如何让多个动画无限顺序播放?

ios - DiskCookieStorage 改变策略从 2 到 0 - 受众网络 NativeAdSample

iphone - 用于播放视频文件的 UIWebView loadHTMLString

ios - 进入后台时暂停WKWebView?

ios - 在 ViewContainer 中隐藏一个带有按钮的 View 容器

ios - iOS8 星级控制

ios - 如何设置UILabel最大长度?

swift - 在 Swift 中是否可以为你的类类型创建隐式转换?