Why view size is 703x768 for iPad app in Interface Builder? - xcode

Im working on my first iPad app.
By default Xcode generated for me a starting point project, which contains the basic template for the Split view controller.
I am focusing on the detail view controller right now (my first app version may only utilize this view).
Anyway, I noticed that in the Xcode integrated Interface Builder the detail view is 703 (width) x 768 (height).
First question: why is the width 703?
Now, the view in the IB shows a portrait layout of the view. So it's even more odd that the width is greater than the height (when it should be the other way around).
I actually expected the width to be 768, and the height 1024.
updated *
2nd Question: In portrait mode, why is the width 1024 and height 768 (see above closing comments).
Thanks.

The split view's master is 320 px wide according to the documentation, leaving the detail pane a comfortable 768 or 704 px wide depending on the orientation of the device.
ref> ipad split view dimension parts
Please take a look here

Related

NSToolbarItem with Segmented Control - Images Not Rendering Correctly

I'm trying to emulate Xcode's toolbar controls to show/hide the Navigator and Inspector:
...but without the bottom pane (only left and right: two segments)
I screen-captured the icons from Xcode's UI and traced them in an image editing application. The resources for the left pane are:
#1x:
(20x20 #72 dpi)
#2x:
(40x40 #72 dpi, although using 20x20 #144 dpi seems to make no difference)
The right-pane counterparts are identical, but flipped horizontally.
All rhe resources are stored in the asset catalogue, as follows:
I dropped a segmented control on the toolbar, to create a toolbar item with a segmented control inside it, and set the image attribute for each segment (0 and 1).
Image Scaling for both segments is set to "Proportionally Down". The segment control has segment width "Fixed" checked, with a width of 48 for both segments. The toolbar item has Minimum Size of (83 x 25) and Maximum Size of (100 x 28).
The icons display correctly on the storyboard (Interface Builder).
However, when I run the app, I can not get the icon images to display appropriately.
If I launch my app on a retina monitor, no icon is displayed on either segment.
If I move the window to the external, non-retina monitor, both icons are displayed.
If I remove the one of the two image sets from thew catalogue and run the app, the other icon is correctly displayed! (on either monitor)
If I further set the same, remaining image for both segments, they display correctly!
If I leave both image sets in the project, but reset the Image field in one of the segments to empty, the other icon isn't displayed eihter!
What on Earth is going on??
I have put a sample project on GitHub that reproduces the issue.
Edit: Just to make sure, I extracted the resources from the compiled app binary using the cartool command line utility (as explained in this answer), and all 4 images are there at the right sizes...
Solution: As suggested by Ivan's answer below, I switched to using vector graphics (PDF) for the icons. I downloaded the trial version of Acorn and recreated my icons at 1x size, then exported as PDF.
To avoid blurring at the scaled up size of #2x at runtime, I had to make sure all coordinates in the editor were integers, and also check the box for "Snap to pixels" in the Vector Shapes inspector for each shape layer:
(Happy Ending)
$ git commit -m "Fix toolbar icons for good (PDF)"
According to my experience, using of bitmaps in toolbar is troublesome. You can try to target exactly recommended resolutions to possibly avoid some problems: https://developer.apple.com/macos/human-interface-guidelines/icons-and-images/custom-icons/
However, the cleanest way would be to use vector (pdf) icons, as they simply work as intended.

Auto layout only returning single device width. Expecting device that is being used width

I'm trying to set my UIImageView corner radius to produce a circular UIImageView. However, when I run my app on an iPhone 7 Plus the image isn't quiet circular.
imageOutlet.layer.cornerRadius = imageOutlet.frame.size.width / 2
While debugging this issue, I noticed that the width returned was the same regardless of the device. Then, when I changed the "view as: iPhone 7 (w C, h R)" near the bottom of the storyboard to iPhone 7 Plus, all worked as expected on iPhone 7 Plus and did not work correctly on iPhone 7.
My constraints for the UIImageView used are as follows:
My intention is to be able to get the width of the UIImageView as it is on the device that is being used.
Add width/height constraints on the UIImageView to force them to be equal. If the width/height of the image is dependent on the screen size, you will get weird results like you described. Since you have a trailing/leading constraint of 42 pixels to the Superview, the width of the image is going to be different depending on the device. The formula you used only works with equal width/height.
After wrestling with this problem for a while I have finally found a solution.
Summary: Calling layoutIfNeeded() on my UIImageView outlet before calling .frame.size.width yields the correct width for the device that is running the application.
Details:
My setAppearance() function is called in the viewDidLoad() function. During viewDidLoad() call, the application returned a width that was based on the default device (in my case: iPhone 7). This happened even when I was running on the iPhone 7 Plus simulator. However, calling width anytime after viewDidLoad() yielded the correct width based on the device that was running the application.
Below, i'm running the application on an iPhone 7s Plus and printing the width during the viewDidLoad function. The width changes from 281.0 (iPhone 7 expected size) to 310.6667 (iPhone 7 Plus expected size) when called again, after the viewDidLoad() fn.
This results in unsatisfactory as my image is not completely circular (my intention with the .frame.size.width/2 call).
However, calling layoutIfNeeded() fn right before accessing the width member of my UIImageView yields the following width during the viewDidLoad() fn.
By forcing the layout of the UIImageView with layoutIfNeeded() before accusing the width, the application receives the correct width based on the device during the viewDidLoad function.
visit https://developer.apple.com/reference/uikit/uiview/1622507-layoutifneeded for more information.

How do I get the auto-layout to work correctly in xcode?

I am attempting to get the auto-layout to work correctly. For some reason, when I change the size of the device, all of the images and buttons are out of place and don't resize at all. This is true for the 3.5 inch screen, as well as the ipad retina. When I run the simulator, the app shows exactly how it shows in storyboard. Auto-layout should be resizing everything so that this does not happen. please help!
The issue is that constraints don't scale. When you set a constraint height to 200 it will always be 200, even if the screen is only 100 high. So what you need to do is try to set constraints that makes the size dynamically rather then set the constraint height/widths yourself.
For instance, on your first label under the UIImageView set the constraints leading and trailing to 20, then set the label constraint to center horizontally on the UIImageView. No other constraints for the label. This will cause the label to shrink in width since its set to be 20 away from each side. The height will not change since there is nothing "pushing" it to shrink since you are missing a few constraints, but basically you need to use the top, bottom, leading, and trailing, constraints to the UIViewController view in order to "scale" the elements.
If you'd like I could try and set the constraints for you if you upload the project and you can then watch how I did.
Edit
It's hard for me to know what kind of behaviour you'd want, but something has to scale or change size in your set up. The easiest would be to resize the UIImageView. It's basically impossible to make it look exactly alike in every screen size, though, so you'll have to play around a bit with constraints until you understand how they work and how to adjust them according to what you want. One way is to create the layout you want and then connect them to code and scale them depending on size of the screen (this is a bit of a pain, I did that in a project recently and it took a bit of time and effort).
The easiest approach is to make the UIImageView to change its size when it needs to, and add extra space in the bottom. Here is a demo project I threw together earlier. Download here
Profile pic
1) first you set the profile pic image center horizontal to the content view
2) set height and width for the profile pic
3)set top space constraints to content view
Change Profile pic label
1)set central horizontal to profile pic
2)set width and height
3)set vertical space between profile pic
Challenges Accepted label
1) set vertical space to profile pic label
2) set Leading space to container
3) set height
and do same for the below two content . i didnt tested. check it and try
After a long night of struggling, I figured out what I needed to do. I was setting the individual view controllers to have a simulated size of 4.7inch. This made it hard for the constraints to scale across all devices since I was setting the constraints for a specific screen size. However, this would be the way to go if using size classes.
I solved my issue by using the standard 600 X 600 canvas that is available before choosing a specific size. This way, I was able to set constraints that scaled down for the smaller screens, and scaled up for the larger/HD screens. This works perfectly with auto-layout. Hope that helps someone!

Using size classes and autolayout in xcode 6

I'm looking for some help with the new storeyboards and size classes in xcode 6. I'm looking to build a fairly simple interface with a few labels and text field, however I'm having problems making this look correct for the different devices.
As you can see from the attached image iphone 6 picks up the constraints and size set for the iphone, which looks daft (forcing the label for Score, for example, closer to the left because of the constraint) and doesn't make use of the screen size. Is there no way in xcode to build a layout and have it proportionately adjust to the screen size? Buttons and labels increase in size by x amount?
I can build completely separately for ipad and iphone and was happy to do this in previous versions of xcode but this seems to be defeating the whole point of size classes, and even if I was to do this my problem is still the iphone 6 and 6 plus which would pick up the iphone layout and look ridiculous.
I'm tearing my hair out with this so would appreciate any help.
Maybe constraint the lower elements to the bottom of the screen rather than the top. Other than that I think you don't have many options.
You can change your layout for different views using compact and regular widths and heights. Click on wAnyhAny at the bottom of your screen. This will provide a grid view pop up, which lets you customize view for different screen sizes. As you select different boxes, the bottom will tell you what size the view applies to.
You can change four things in a size class:
1. The constant for a constraint
2. If a constraint is installed in the view hierarchy
3. If a view is installed in the view hierarchy
4. The font used for many types of views showing text
This means if you want a different label or constraint for your iPad view, you can add that constraint just for that view using size classes.

grow or shrink UIImageView frame to fit the entire iPhone screen via AutoLayout?

So I'm still getting accustomed to the world of Auto Layout in iOS 6 and it's been a fun (or in plain English -- tough) migration coming over from strings & struts.
I have a UIImageView that's the background for a game I'm working on. Here's what a regular 3.5" Retina Display looks like in Interface Builder:
but if I change the "Size" pop-up in the Simulated Metrics field for the content view to "Retina 4 Full Screen", here's what I see:
And you can see the ugly black bar appearing along the bottom edge of the simulated iPhone 5 screen. This same ugly black bar makes it over to the compiled app running in the iPhone 5 simulator.
Are there any attributes or constraints I can apply via Interface Builder to get the UIImageView's frame to size correctly for the appropriate iPhone device screen size?
Or do I have to enter in constraints via code? (ugh)
I've watched the three WWDC videos and if the engineers covered the topic of sizing a view to fit a parent, they must have glossed over it really fast because I've haven't yet found or heard a decent method to get both UIImageView and NSImageView to size correctly to their parent views under both the iOS and MacOS side.
From what you're showing here, I think that what you want to do is drag the image to meet the bottom of the superview so that it takes up all of the space you want. On the bottom right in the IB view you will see a small, grey pill-shaped set of 3 buttons. Click the center one (once you have your imageView selected). It's the one that looks a little like this: |--|. It will bring up a list of constraints. Once you have done that, select "Bottom Space to Superview". You should then be able to switch between phones and have the image resize automatically.

Resources