如何在集合视图的顶部添加自定义视图? 我想使用Xcode用户界面设计自定义视图 我在storyBoard视图控制器中添加了我的视图,但集合视图覆盖了它 所以我在viewDidLoad
中添加了一个自定义视图- (void)viewDidLoad {
[super viewDidLoad];
[self addViewOnTop];
}
}
-(void)addViewOnTop {
UIView *selectableView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, 40)];
selectableView.backgroundColor = [UIColor redColor];
UILabel *randomViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 100, 16)];
randomViewLabel.text = @"RandomView";
[selectableView addSubview:randomViewLabel];
[self.view addSubview:selectableView];
}
此代码成功运行,但如何使用Xcode界面构建器加载视图或加载.XIB文件
现在我这样做
@implementation chatViewController
- (void)viewDidLoad {
[super viewDidLoad];
[SVProgressHUD dismiss];
self.collectionView.collectionViewLayout.sectionInset = UIEdgeInsetsMake(70, 0, 0, 0);
self.collectionView.dataSource = self;
self.collectionView.delegate= self;
messages = [NSMutableArray array];
[self addMessages];
self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero;
self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero;
self.topContentAdditionalInset = 70.0f;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCollectionTapRecognizer:)];
[self.collectionView addGestureRecognizer:tapRecognizer];
self.collectionView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[self addViewOnTop];
}
-(void)addViewOnTop {
commentsheader *test = [[commentsheader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 70)];
[self.view addSubview:test];
}
但它没有帮助
答案 0 :(得分:0)
如果我理解你遇到的真正问题是你的customView
覆盖collectionView
而不是修改故事板,只需调整集合视图的插图。
添加UIEdgeInset以便您在新视图下查看消息在viewDidLoad()中调用此消息
self.collectionView?.collectionViewLayout.sectionInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
答案 1 :(得分:0)
您应该尝试currentView.bringSubview(toFront: <UIView instance>)
答案 2 :(得分:0)
问题是导航栏隐藏了视图。 但把它带到前面并没有解决问题 但这样做修复了它: 在viewDidLoad
中self.edgesForExtendedLayout = UIRectEdgeNone;