UITableView empty view does not fit - ios

I've created an empty view .xib file to be displayed when the application is unable to download certain data from the internet.
The problem I'm facing is that when I set the empty view as my tableView's backgroundView, the empty view does not fit precisely into the screen.
I'm not sure why this is happening.
-(void) updateUI
{
if ([self.categories count]) {
self.tableView.backgroundView = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}else{
NSArray* nibs = [[NSBundle mainBundle] loadNibNamed:#"EmptyView" owner:self options:nil];
UIView* emptyView = [nibs objectAtIndex:0];
[emptyView sizeToFit];
self.tableView.backgroundView = emptyView;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
}
I tried [emptyView sizeToFit] which didn't work. I also tried setting the frame of the empty view but I had no luck with that either. I'm fairly new to iOS development so I might have missed something really basic.
Clarification:
I did set Autolayout constraints on both labels in the Empty View:
The 'No categories available' is set to be in the centre of the view.
The instructions are set to appear a standard distance below.

You can use Auto Layout to set some constrains to the view, make sure that will fill the whole screen.
This tutorial of Auto Layout may help.
http://www.appcoda.com/introduction-auto-layout

Related

Black screen while trying to set UITableView background to a UIView from xib

I am trying to set a UIView (loaded from a xib file) as the background of a tableview. But when running the app on a device it is just showing a black screen and there are no errors. This is how i do it,
zoneListEmptyView = [[[NSBundle mainBundle] loadNibNamed:#"TableViewEmptyState" owner:self options:nil] lastObject];
zoneListEmptyView.bounds = self.view.bounds;
And in numberOfSectionsInTableView: i check if data to be displayed is empty and if yes, i do,
[self.tableView setBackgroundView:zoneListEmptyView];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
I checked other similar questions in stack overflow and tried to recreate the xib and class files, but no help. I am displaying tableview empty states in another application using the same approach and it works fine,but not here. Please help

Want xib file to cover whole screen instead of covering top left ios

I am making an ios app and using xib files instead of storyboards. At first I didn't even have to use auto layout everything worked fine for all iphone devices in portrait. But as soon as I added a launch screen through assests only the the iphone 5 has the correct layout (because xib file size is set to 4 inch). But any greater screen size and only the top left hand corner is covered like below:
I want the view to cover the whole screen. There is a view at the top but I cannot add any constraints to it. Inside the view there is an imageview that is supposed to cover the whole screen. So I added 4 constraints and pinned the 4 sides to 0 so the imageview should cover the whole screen. But it still does not work. What can I do so the whole screen will be covered?
Have you tried set the frame?
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"xibFileName" owner:nil options:nil];
[[array objectAtIndex:0] setFrame:self.view.frame]; // Trick here: set the main view frame
This worked in my case:
// Create a Container
UIView *container = [[UIView alloc] initWithFrame:self.view.frame];
container.backgroundColor = [UIColor clearColor];
// Add .xib file on top of the screen
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"xibFileName" owner:nil options:nil];
[container addSubview:[array objectAtIndex:0]];
// Add container to main view
[self.view addSubview:container];
About the itens inside of .xib file you have to set the constraints there.

Additional View getting inserted into UICollectionView loaded from Xib

I am having a weird issue with using a Xib for UICollectionViewCells. The issue is that an additional view is being created, that seems to be on top of everything, rendering my gestures and IBActions useless.
Here's my setup:
I have a UIViewController with a UICollectionView in it.
In the storyboard, the UICollectionView has a single cell, of class "MyCell", with reuse id "cell"
In cellForItemAtIndexPath, I only return the result of [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath]. I also hardcoded 30 items in numberOfItemsInSection.
That's that. Besides that, I have MyCell.h, MyCell.m and MyCell.xib.
The xib contains a single UIButton, whose Touch Up Inside event is set to call the buttonPressed IBAction. The view itself is set to the class MyCell and the tag is set to 123 (we'll get to why below).
In MyCell.m, I overrode awakeAfterUsingCoder to return [MyCell initView:self]. The definition of initView is at the bottom. But it basically loads the view from the Xib.
That's it.
When I run the app, 30 cells show up, all with their button, but when the button is pressed, nothing happens. After a lot of investigating, I found that there's an extra UIView added inside the cell, which is on top of the button, and it's covering the entire thing.
If I add the for-loop below inside awakeAfterUsingCoder, then the button works again.
- (MyCell *)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
MyCell *cell = [MyCell initView:self];
for(UIView *v in cell.subviews){
if(![v isKindOfClass:[UIButton class]]) [v removeFromSuperview];
}
return cell;
}
My question is: what is that view, and why is it there??? Even though I can get everything to work by removing that view, it feels like a hack.
Thank you! And I can answer any other questions if necessary.
+ (id)initView:(UIView *)viewToInit
{
UIView *viewToReturn;
if(viewToInit.tag != 123){
//If we're in the storyboard codepath
//Initialize from the xib.
//If the code below is failing, we probably forgot the 666 tag :/
viewToReturn = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([viewToInit class])
owner:nil
options:nil] firstObject];
//copy frame, autoresizing, and layour items.
viewToReturn.frame = viewToInit.frame;
viewToReturn.autoresizingMask = viewToInit.autoresizingMask;
viewToReturn.translatesAutoresizingMaskIntoConstraints = viewToInit.translatesAutoresizingMaskIntoConstraints;
for (NSLayoutConstraint *constraint in viewToInit.constraints){
id firstItem = constraint.firstItem;
if (firstItem == viewToInit){
firstItem = viewToReturn;
}
id secondItem = constraint.secondItem;
if (secondItem == viewToInit){
secondItem = viewToReturn;
}
[viewToReturn addConstraint:[NSLayoutConstraint constraintWithItem:firstItem
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:secondItem
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant]];
}
}else{
//otherwise do nothing and just return what was passed in
viewToReturn = viewToInit;
}
return viewToReturn;
}
I've just had this problem myself, and the answer lies in the cell nib file. If you've created a nib and are using the stock UIView contained as a cell, that's where your problem is. This UIView needs to be deleted immediately and replaced with a newly-dragged-on UICollectionViewCell. This will then be your cell, and you can add views to your hearts content without further issues.
OK, so after a long time of having a nasty hack in there to work around this, I've figured it out.
The issue is that the contentView of the UICollectionViewCell is getting inserted on top of my views. It is very weird to me because this doesn't happen with UITableViewCells which also have a content view.
What I did then, was to put everything in a "container" view and then move that container view into the contentView on init, and that took care of the problem.
I still don't understand WHY this is happening. It seems to me that all my views should be added into the contentView (like the table's cells do), but at least I can now work around it.

Resize imageview in its frame

I am trying to create a view that has 4 children
1 imageview
3 labels
The ImageView is supposed to take up the entire frame, and it doesnt
I've added layout constraints to it so that it would adjust to the sides.
However, i am initiating the view with different frames, and the image view size doesnt change
How do i make the image take up the entire purple area, of which it is a subview?
This is my xib file:
and this is how it looks on the device screen
the purple view is the frame i allocated for the given view:
and the green view is the entire screen
my init code:
NSArray *topViews = [[NSBundle mainBundle]loadNibNamed:#"BagView" owner:self options:nil];
[self addSubview:topViews[0]];
[self.shoppingBag setFrame:self.bounds];
NSArray *topViews = [[NSBundle mainBundle]loadNibNamed:#"BagView" owner:self options:nil];
UIView *v = topViews[0];
v.frame = self.bounds;
[self addSubview:v];
fixed the problem
Set the contentMode property of UIIMageView to UIViewContentModeScaleToFill
[your_imageview setContentMode: UIViewContentModeScaleToFill];

Font and textColor are gone - Yet another UITableViewCell awakeFromNib

I don't know if this is specific to UITableViewCells but rather general for UIViews (as i believe) but i noticed the problem with a cell.
As mentioned, I have a custom UITableViewCell subclass which loads itself from a xib when it gets initialized with an designated init:
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:nil options:nil];
self = nib[0];
if (self) {
// Initialization code
_reuseIdentifier = reuseIdentifier;
}
return self;
}
It has a UILabel as an outlet and I set some properties of it in the awakeFromNib method:
- (void)awakeFromNib{
[super awakeFromNib];
self.labelLeft.textColor = [UIColor grayColor];
self.labelLeft.font = [UIFont boldSystemFontOfSize:15.0f];
}
The point is, that the label doesn't keep the textColor nor the font and I don't understand why.
awakeFromNib gets called and the outlet is connected right, since i can set the text.
I can make it work by settings those properties after I set his text in the UITableViewDataSource but I don't feel it's the right way and I want to understand why this doesn't work.
Question:
Why doesn't it keep the Font and Textcolor and what can I do to make it work the right way?
I have the same issue with all my labels loaded from .Nib's. Then I found that the problem was in UIAppearance. I have the following line in my AppDelegate:
+ (void)styleApplication {
[[UILabel appearance] setFont:[MGStylesheet defaultLightFontOfSize:17]];
}
This method update all fonts in my application, and all works fine, until I start loading my UIView's from .Nib. When I removed this, everything start working as expected.
Set the attributes of the label in the nib-file. If you split your interface configuration to different places this causes just trubble.
If you don't want to do that move the configuration of the label into viewDidLoad:

Resources