iOS Constraints of image view inside table view cell gets misplaced on scroll - ios

The constraints for the image view inside Table view cell gets distorted on scroll.
The constraints are fine on initial load and view. Once the table view cell goes out of the view and we scroll back to get it, the image view gets displaced.
This is the screenshot of single TableViewCell. The globe icon is misplaced from its original spot (left of name and designation) and overlaps on the text below.
Screenshot of prototype cell from storyboard
The black lines show the constraints added on the storyboard.
Using XCode 8.0 with Swift 3.
I am doing something wrong with the constraints ?

It seems that you are missing a height constraint on the image view. Should be able to fix problem if you add that.
Also, maybe related as well, can you set your image view's content mode to aspect fit? Sometime like this:
ObjC
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
Swift
self.imageView.contentMode = .scaleAspectFit

Related

Why I cannot apply constraints to the photo in the UITableView header?

In my storyboard I have a UITableView and above the dynamic cells I added a UIView to keep there a photo and some labels.
Currently, I want to apply constraints to the photo so that it reaches the top, left and right side of the screen and keeps the aspect ratio. However, the storyboard looks like this:
The constraints for photo are:
However, as you see, the constraints are marked in red and when I run the app the photo is not stretched properly.
What is causing this issue?
I would probably do this programmatically. It is pretty simple to create a custom view and set it as your header view. You can either create a UIView with a UIImageView inside of viewForHeader or by assigning it to the entire table with tableView.tableHeaderView = headerView.

How to give constraints to a tableview when tableview cell is on xib. And table view is on storyboard

I have a table view on storyboard i.e. FirstStory.Storyboard. And for this tableview I have created a table view cell on xib file where I give size to its width as per my current storyboard width. But when I launch my app it runs fine on the landscape position. But when I rotate it in the portrait mode. Width is unable to show the all item which I placed in tableviewcell.xib. How we mutually combine these two item in storyboard (table view on storyboard while cell on xib). Which follow a single constraints with above condition.
UITableViewCell's contentView property is always the size of the cell. So you have to setup the constraints of your UI components in the xib file with the Cell's contentView properly. The issue you are facing must be because you did not setup these constraints properly.
To see which views or constraints have been placed wrong, use the XCode's 'Debug View Hierarcy' feature.
Launch it from here
Run your app, navigate to your tableView, then press the above button to launch the view debugger. It provides very good insight into your constraints and where the issue may lie

Difference in constraints iOS 7 and iOS 8. Xcode 6 seed

So I have a UIViewController with a UIImageView and a Collection View, as well as a Navigation Bar. This app is only in portrait mode and won't change when rotating.
in iOS 7.1.2 and below, it shows up correctly, like this:
image1 http://imageshack.com/a/img911/4185/koGDtt.jpg
In iOS 8, it shows up incorrectly where the collection view is at the very top of the screen behind the navigation bar, like this:
image2 http://imageshack.com/a/img673/2531/NWYWsn.jpg
I have been trying for a couple days now, varying ways of constraining the collection view and it has always yielded the same results. I have searched SO for an answer that may help me resolve this issue but have not been able to come up with something that worked.
I am generally constraining the top of the collection view to the superview (330), the leading and trailing edges to the edges of the superview (0), and then height/width constraints on the UICollectionView to it's dimensions (320, 80).
I tried making a new UIView in IB, constraining it to the outer edges of the superview, and then constraining the collectionView to this new UIView, but it still did the same thing. I'm at a loss here and really appreciate any help you all can give me.
So I figured this out finally. It was completely unrelated to constraints and was in fact a line of code that displaced the entire UICollectionView in iOS 8.
In the -didSelectItemAtIndexPath method I made a call to update the selected indexPath of the collectionView using this line:
[collectionView reloadItemsAtIndexPaths:indexPaths];
I was also pre-selecting an cell at indexPath 0 in my -viewDidLoad so that the first item of the collection would be highlighted, which caused the -didSelectItemAtIndexPath to be called right away.
For some reason, the reloadItemsAtIndexPaths line would change the view of my entire collectionView to the top of the screen.
I replaced this line with:
[collectionView reloadData];
and now everything works fine as intended.

iOS: Stretching ImageView Above My TableView?

I have added a UIImageView on top of my tableView in storyboard & it works perfectly fine, except that when you scroll down, the imageView doesn't stick to the navigationBar and instead only sticks on to the tableView (revealing the view's background above it).
How would I get it to stick to both the tableView and the navigationBar, and just have the imageView stretch/zoom as the user pulls the tableView down?
This is how it's set up in storyboard:
And this is how I assign an image to it in my ViewDidLoad:
childHeaderView.image = [UIImage imageNamed:headerImage];
Any help would be greatly appreciated. I've tried setting constraints on it using autoLayout but it doesn't seem to let me (they're grayed out, even though I've enabled it for that ViewController).
The problem is that what you did in storyboard is adding the UIImageView as a header to your UITableView.
The proper way to do this is to add the UIImageView at the same level as the UITableView, which mean embed these two views inside a UIView.
Having a UIView as a root view for a view controller is unfortunately impossible for a UITableViewController, and I fear that this is your case. So you may want to replace your UITableViewController subclass by a UIViewController subclass.
EDIT: You'll want to set a fixed height constraint on your UIImageView, and add a vertical space constraint with a 0pt value between your UIImageView and UITableView.
Most of these can be achieved by moving view in IB.

iPhone + resize UITableView

I have a view in which I have UITableView (grouped style). In the Interface builder, I am resizing the UITableView so that it looks like a small portion in center of the screen.
But when I run the application, UITableView takes up the whole area of screen and does not look like the small portion in center of screen (which I had set in Interface builder).
I tried to resize the tableView programmatically in the viewDidLoad method as tableView.frame = CGRectMake (0.0,0.0,100.0,100.0), but still the tableView occupies the whole area of screen.
Please help.
Regards,
Pratik
Sounds like your table view's got autoresized. Try to fiddle with the autoresizing settings.
If your table view is the main view then it will automatically fill the whole view controller's space regardless of the autoresizing settings. In that case, make an empty UIView as the root view and put the UITableView as a subview of it.

Resources