UIButtons not working on certain devices - ios

I've set up a view and constraints using the wAny hAny layout in Xcode 6:
When I run the app on a iPhone 5s and iPhone 6 the UI Elements are all visible but on the 5s the answer and the back buttons do not work - they do nothing.
On the iPhone 6 all the buttons work.
Here's the code for the `back' button:
- (IBAction)backFromDivision:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
The answer code is a bit longer and I won't post it unless necessary.
Does this have something to do with the constraints or should I be looking for answers elsewhere?

Well,
I clicked on "Resolve Auto Layout Issues" - "Reset to suggested constraints" (third from the left):
and that "solved" my problem. Everything is working on all devices. It seems to have removed some constraints that I put in place.
I'm going to have to learn more about why this worked.

I've had a similar issue before. It was the constraints that were the issue and the fact that the button was not in the view's bounds.

I've encountered this issue, and the problem was due to the button having:
buttonView.frame.height = 0. (it can also happen with width)
The problem is that even with no height the button text appears on screen so if you don't have a background the button visual appearance does not change.
To resolve this issue I played with the views Content Compression Resistance Priority(CCRP) of different views to chose which view should resize on smaller screen.
The default CCRP of views is 750, default contraint priority is 1000.
So to match your constraint ios may resize the height of your views

Related

Auto Layout constraints for container view inside UITableView header broken for different devices

I have an issue that makes no sense to me. I have the following setup:
| UITableView |
|| UITableView header ||
||| UIView |||
So inside my UITableView header I have a container view that has leading, trailing and top constraints to its superview. Everything is set up correctly for my test device size (which is iPhone 8). If I change the test device to iPhone 8 Plus I get a strange offset for my trailing constraint - 39pt to the right edge, which is exactly the difference between iPhone 8 plus width in points and iPhone 8.
When I switch between devices in Xcode and see that the trailing constraint is not correct I just make an adjustment myself (change trailing to 1 and then back to 0) and the problem goes away for the particular device.
Initially I thought it is bug in Xcode but when I tested on a device the problem is still there.
I tried setting up a new view controller and adding the same elements but with no effect.
I am attaching screenshots to make my issue clearer.
Before: Adjusting the desired constraints
After: Switching to a device with different size
Adding this as an answer because it's too much for a comment - even though it's really just a confirmation, not a solution.
OK - looked at your project.
I'd say it's an IB / Storyboard bug, which I've seen in other circumstances. If you change the View As... device, the frame does not update immediately.
However, if you change anything that would cause a layout update - such as temporarily changing the background color of a view or font size of a label - everything should snap into place.
You'll also notice that if you select an element and move it slightly, the Update Frames button / menu item becomes enabled... and that will also correctly update the frames.
Note: When I ran the app, regardless of how the layout looked in Storyboard, the constraints correctly sized the views at run-time.
At first I accepted DonMag's answer (thanks for your time) as this really seemed to be an Xcode Interface Builder bug. As I investigated further when having the scenario I mentioned auto layout constraints are not updating the layout when I need it hence not giving me the right view.bounds.
I tried getting it in viewDidLayoutSubviews() without success as well - it was still giving me a size that suits another device.
What did the trick was calling view.layoutIfNeeded() before working with view's bounds. What it does is to update the view's layout immediately. As a result you can access the desired view's bounds.

Issue displaying uitableview in UIViewController - Not aligned

I dragged a uitableview component to my UIViewController in the Storyboard.
It appears as
Then when I ran the application, there's a space on the side. How to prevent this?
Note: Initially this application was enabled autoLayout, and I removed it later on. Hope there's no effect for this error.
Add constraints on your tableview.
If you don't know how to, here is a simple step by step :
Right clic or ctrl+drag the tableview (on the left side list) to
View.
Select Top space to top layout guide Repeat with Bottom
space to bottom layout guide
Repeat with Leading space to
container margin
Repeat with Trailing space to container margin
That should fix your issues.
The answer on this link will surely help you.
https://stackoverflow.com/a/26708486/3396270
Though its Table view in your case you can use it with all the components by changing the settings as per their requirements.
Check your screen size in Storyboard:
And make sure you are running the application on same size of Simulator as in your storyboard.
If you uncheck the Translucent property of your Navigation Bar, it's going to fix it.
If you are not using AutoLayout then just write this code in your viewDidLoad Method
[tableView setAutoresizingMask:UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight];
You could also set the AutoresizingMask from your nib file.
What version of the xcode are you using? If it's okay with 5s and not okay with 6 and 6+ then you should change the size of the table manually or use autolayout. Storyboard which you'v used should have optimised for devices below 6. I you're using #3x image for splash screen then your app supports 6 and 6+ but it uses the same table size as what it is designed for 5s device size on simulator.
Solutions-
Use auto layout so this will solve the issue itself.
Else create outlet for table and changes it size according to the device width and height.
I have released an app using the above 2 solutions and its in the store now.

Autolayout problems with iOS8 with code that works fine on iOS7

I'm in the midst of developing an app for the iPhone and iPad. It supports iOS6 and iOS7 and it uses auto layout exclusively.
This past week, when Apple announced that iOS8 was ready for prime-time, I upgraded one of my iPhones and an iPad both to iOS8. I also bumped my XCODE up to version 6. I have 2nd iPhone which I left at iOS7.
I generated new executables with Xcode 6 and I was distressed to see that their screen layouts were messed up when executed on my devices running iOS8 but still fine on iOS7. This is true on both my physical devices and on Xcode's emulators.
It took a lot of digging but I'm pretty clear now on what's happening though I don't know why.
Specifically, certain auto layout operations are failing for me on iOS8 but they are fine on iOS7.
Some examples involving a button which I am placing on an underlying view whose size is equal to the size of the screen:
(1) If I ask auto layout to position the button's horizontal center (CX) equal to the underlying view's horizontal center, the result is that the button's horizontal center is placed on the underlying view's left edge.
(2) If I ask auto layout to to make the width of the button equal to 50% of the width of the underlying view, it gives it no width at all.
I am able to work around these issues as follows:
(1) I ask auto layout to position the button's center equal to the underlying view's left edge plus 50% of the screen's width.
(2) I ask auto layout to make the button's width equal to 50% of the screen's width.
I am slowly clawing my way, with workarounds like these, back to auto layout code that works for me on both iOS7 and iOS8. But I am really wondering what's going on here.
It looks like auto layout cannot determine the size of the underlying view and so auto layout calculations that require that information fail. But it does know where the top and left edges of the view are so calculations based on those data succeed.
This is a large app and I've written many hundreds of lines of auto layout code for iOS6 and iOS7 that work perfectly for me.
I've been tweaking and trying things now with iOS8 for three days and I'm no wiser than I was when I began.
Anyone have any suggestions or thoughts as to what might be the issue here?
#robmayoff has a great answer for this: https://stackoverflow.com/a/26066992/1424669
Essentially, in iOS8 you can no longer call setNeedsUpdateConstraints and setNeedsLayout on a view and expect the constraints of subviews to update.
You must call these methods on the view whose constraint is changing. This is backwards compatible to iOS7.
EXAMPLE:
Suppose you have a ViewController with root view self.view and a subview called containerView. containerView has a NSLayoutConstraint attached to it that you want to change (in this case, top space).
In iOS7 you could update all constraints in a VC by requesting a new layout for the root view:
self.containerView_TopSpace.constant = 0;
[self.view setNeedsUpdateConstraints];
[self.view setNeedsLayout];
In iOS8 you need to request layouts on the containerView:
self.containerView_TopSpace.constant = 0;
[self.containerView setNeedsUpdateConstraints];
[self.containerView setNeedsLayout];
You might find the answers to this question helpful: UICollectionView cell subviews do not resize
In most cases the works in iOS7 but not on iOS 8 auto layout problems seem to stem from the root view not being sized correctly in iOS 8, particularly when we set translatesAutoresizingMaskIntoConstraints to NO. For my views I was able to set the root view's frame in layoutSubviews (or whichever appropriate initializer that does have the correct bounds) and this resolved the issue.
self.contentView.frame = CGRectInset(self.bounds, 0, 0);
As shown in the answer above, you could also do
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
and then turn translatesAutoresizingMaskIntoConstraints back to NO before you start setting your own constraints in code.
Definitely hate that so much of our time is taken with these annoying gotchas.
In my case, the problem related to constraints labeled UIView-Encapsulated-Layout-Width and UIView-Encapsulated-Layout-Height. When I removed them, everything behaved as though my view was of zero size, with everything centered on the upper left corner of the screen. When I left them in, new constraints worked as expected. I also retained the constraints labeled _UILayoutSupportConstraint.

Auto Layout views inoperable in iOS 6

I'm using Auto Layout for an iPhone app to place a button footer bar at the bottom of the screen on both 3.5" and 4" iPhones. The bar is a UIView containing two UIButtons and a UIImageView.
I have a Vertical Space Constraint pinning the bottom space to the superview.
(Editor -> Pin -> Bottom Space to Superview)
It has an Equal relationship. Constant is 60, Standard is NO, Priority is 1000, and Placeholder is NO.
In iOS 7 it works exactly as expected. In iOS 6, it does not. The bar is not a consistent distance from the bottom when I switch between that view controller and its neighbors, and the buttons frequently do not work (they don't even highlight when I tap them). This is all in the simulator, I haven't tried hardware yet. The app is a legacy app, so all of the layout work is in xib files, not storyboards.
Do I need to do something different in iOS 6 to get Auto Layout to work? From what I read, it should be fully compatible with 6 & 7.
Some more information is needed to really get to the heart of the matter. Specifically how auto layout is being used to attach the bar to the bottom.
One thing that gets me over and over though, is remembering to set the translatesAutoResizingMasksIntoConstraints property to false on view(s) that utilize constraints.
Be sure to check the log output while debugging as that will indicate if you have any ambiguous or unsatisfiable constraints. At design time, XCode 4 IB will attempt to add any constraints that you are missing for you. XCode 5 IB will only tell you what you are missing and then at runtime try to add any missing constraints if it can.

Layout messed up on iPhone 5

I use MainWindow.xib for the layout of my app. I set everything on the view controller attributes simulated metrics to iPhone 3.5 retina. There is no image set for the iPhone 5 Default, yet it still tries to run the app full screen on an iPhone 5, causing the screen to be all out of sorts. Any thoughts on fixing this?
Here is on 3.5" screen:
Here is on 4" screen:
You need the constraints on your labels and fields to be connected to the top of the view, not the bottom. By default, IB pins things to the closest edge. Use the pinning menu to achieve this, then delete the constraints that connect them to the bottom.
Alternatively you can switch to the 4" layout in IB and set things up there.
I have written about editing constraints in interface builder here, hopefully that will be useful to you.
As for fullscreen mode, I think Xcode will put -568.png image for you, you might want to check?
As for fixing the layout, it seems that all you need is to fix top strut on all text fields, so the top position is always the same.
set UITextField autoresizingMask to UIViewAutoresizingFlexibleBottomMargin
recipientLabel.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
...

Resources