0

I'am drawing collection view programmatically below the custom navigation bar but it starts from top if its frame change like this : CGRectMake(0.0,64.0 ,self.view.frame.size.width , self.view.frame.size.height) then it shows black screen on top i have attached screen shot and code also.Please tell me how can i start frame after navigation bar and remove black screen enter image description here:-

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionView_=[[UICollectionView alloc] initWithFrame:CGRectMake(0.0,64.0 ,self.view.frame.size.width , self.view.frame.size.height) collectionViewLayout:layout];


[collectionView_ setDataSource:self];
[collectionView_ setDelegate:self];

[collectionView_ registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[collectionView_ setBackgroundColor:[UIColor redColor]];

[self.view addSubview:collectionView_];
Garry
  • 41
  • 7
  • Possible duplicate of [Cell spacing in UICollectionView](http://stackoverflow.com/questions/17229350/cell-spacing-in-uicollectionview) – Bhavin Bhadani Jul 18 '16 at 08:20
  • Probably you dont need to do this `self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ` – Anil Varghese Jul 18 '16 at 08:20

3 Answers3

0

Remove:

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

Otherwise you move the self.view under the Navigation Bar, that's why you see black.. not because the UICollectionView. You never change the self.view frame of a UIViewController

If you set your Navigation Bar to opaque, your origin point will be automatically under the Navigation Bar. You can change it with:

self.navigationController.navigationBar.translucent = NO;

So the frame of your UICollectionView will be:

CGRectMake(0.0, 0.0 ,self.view.frame.size.width , self.view.frame.size.height);
Jacopo Penzo
  • 2,168
  • 2
  • 24
  • 29
0

Try looking at automaticallyAdjustsScrollViewInsets - It seems like you're trying to manually account for the navigation bar whilst the system is also trying to account for the navigation bar. If you want to do it yourself, you need to tell the system no to by calling:

self.automaticallyAdjustsScrollViewInsets = false;

from your view controller.

See the documentation for more information.

Matthew Hallatt
  • 1,310
  • 12
  • 24
0

There is nothing wrong with code actually, its just a bug.

collectionView_=[[UICollectionView alloc] initWithFrame:CGRectMake(0.0,64.0 ,self.view.frame.size.width , self.view.frame.size.height) collectionViewLayout:layout];

this line will create collection view y position 64 from top. that is correct when there is navigation bar. but from image i can see that there is no navigation bar present in your controller. So please add navigation bar or simply change as stated in above answer. ie set UICollection view's from with respect to UIScreen.

self.view = [[UIView alloc] initWithFrame:[self.view bounds]];

and you remove

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

as stated by @Jacopo Penzo

hope this works.

Devang Tandel
  • 2,988
  • 1
  • 21
  • 43