How to adjust apps UI to different screen size of iPhone models? - swift

I find one solution is using extension to detect current iPhone model at runtime; however, it indeed involves massive of changes from my current code and design (i.e. change a lot of buttons location and size in single storyboard, a lot of constraints need to be updated as well etc). Are there any better solutions than this one? Thank you!

I always vote for creating constraints in IB as possible as you can , because it cuts time less than in code as you'll see the result instantly and do constraints fastly , the tip is to create it proportional as much as you can , look to the current device you do the constraints with as a generic device for example instead of setting a static height like 200 , look to design scale an make it proportional say 0.2 of screen height , same for width , you can make it also with origin constraints like top , leading but it needs some advanced constraints , make use of size classes and adaptive layout , also font adjustment , if all that fails hook the constraint and play with it in code

Related

How to find the largest size of a view that does not make window larger than screen size?

I'm working on a MacOS application that needs to display large images. if I naively set the ImageView to be the full size image, the application's window can be forced to become larger than the screen size. What I'd like to be able to do is work out how large I can make the image while keeping the entire window (which also contains other UI elements) on screen.
I know I can query the amount of available screen space using NSScreen:visibleFrame() but that does not seem to be much help since unless I make a whole load of assumptions about how much space the rest of the UI will take up which then defeats the point of having constraint based UI layout.
The other approach could be to find a way to constrain the window size and then let Cocoa work out the sizes of the views. However, it looks like the UI editors in Xcode only allows static sizes to be specified which is not much help.
It looks like this is possible in SwiftUI (https://www.hackingwithswift.com/quick-start/swiftui/how-to-adjust-the-way-an-image-is-fitted-to-its-space)
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
However, the application I'm working on needs to run on older versions of MacOS than supported by SwiftUI so I need to know how to do this in swift but using plain Cocoa.
The first thing I would try is to modify the priority of the constraints that make the image view large enough to accommodate the image. That would be its content compression resistance priority. I think if you set it to .dragThatCanResizeWindow then you might get the behavior you want for free. That's because the system already constrains a window to the screen size when you drag its edges. So, presumably, that implicit constraint has a higher priority than .dragThatCanResizeWindow.
If that doesn't do it, you can programmatically set a window's maximum frame size by setting its maxSize property. You'll want to set that each time the screen configuration changes or the window moves to a different screen. For the former, you can observe NSApplication.didChangeScreenParametersNotification. For the latter, your window delegate can implement windowDidChangeScreen(_:) or you can observe NSWindow.didChangeScreenNotification.

Auto Layout Swift 4 Xcode 9 For Multiple Devices - Am I doing it Wrong?

I am having severe difficulties finding a clear working world example of auto layout on multiple screen sizes. Most tutorials have basic stuff like a button thats centralised and then after adding constraints in Interface Builder it scales with the device. One button is simple but in my example for the iPhone 4:
There are quite a lot of labels / buttons and some that are not central. The above screen shot is exactly how I want it to look on all devices.
The app is for portrait only but I want to target all devices. I tried slowly adding constraints to items from the bottom up using a multiple of the superview to position and size items and then applying aspect ratio to UI Objects (got as far as the got it button but not the red arrow buttons) but as you can see in the next picture for the iPhone 8 plus... the white space isn't going to perfectly fill like in the iPhone 4 which will leave me with a big gap at the top.
Is there a way I can change just the iPhone 8 plus layout in Interface Builder to build a layout that caters for the extra screen size?
The iPhone 8, SE and X layouts are equally awkward with extra white space.
I tried size classes but that changes it for all phones.
Is there a way to centre what I have done so far?
How do you guys position objects in auto layout that are not centred?
Am I going about it the wrong way by applying X / Y constraints using a multiple of the superview? - same for the width. I'm only specifying widths as a multiple of the superviews width then adding aspect ratio.
I just need a real world working example I can learn the basics from or a system to create for multiple devices that I can follow. I don't want to use a 3rd party library.
I really hope someone can help point me in the right direction!
Krivvenz.

Scale Image to image view

I am currently building an iOS app. I'm still in the very beginner stage but know my way around programming.
However the Interface Builder gives me a headache.
I want to have an image view and then programatically load the image content (no problem here). However I want the image view never to change the initial size (now matter what the resolution of the image is).
Here is the trick though: The image view should be a certain distance from the left, right and top. On the bottom the imageview needs to keep a minimum distance but can be as large as possible (for different iPhones).
I am still lost what mode I need to set for the imageview, because currently every single setting resizes the view somehow.
Anybody got any advice on how to set the mode and the constraints?
You need to implement windowWillResize (or the live variant) from NSWindowDelegate and calculate the scale for the image in the view before re-displaying it.

Auto layout if orientation is not an issue?

I am currently trying to get the hang of Auto-layout in Xcode, and with it, i have found some problems.
Now, i don't have a 100% grasp on how auto-layout works exactly, so as i learn on it might become easy, but i think it is very hard to be honest. I am usually making games, and of thus, i programmatically add a lot of views to the screen. Now, auto-layout seems to mess everything up when i do this, so i constantly have to make sure that every single auto-layout feature is just right, and there are a bunch!
So, if you do not want to support other orientations, is auto-layout really needed? i mean, if i am making a game for the iPad and using landscape mode only, is there any reason for me to use auto-layout?
Thanks on advance
A couple of thoughts:
Auto layout is not mandatory.
But auto layout provides a mechanism for dealing with situations when views change sizes and/or locations:
changing orientation, as you pointed out;
supporting different screen sizes (e.g. iPhone 3.5" screen vs 4.0" screen);
controls whose size changes based upon content (e.g. a UILabel whose width expands to fit the text or whose lineCount is set to zero, meaning that its vertical will grow based the number of lines needed to fit all the text); with auto layout you can establish dependencies between controls so that, for example, as one control changes its own size/location, others can move/resize accordingly (e.g. to stay aligned with or make room for the other control); and
automatically update contentSize for UIScrollView objects.
Auto layout requires a little time to gain proficiency, but for dynamic, complex user interfaces, it can save you from having to write code to manually change layouts of views programmatically. But if you don't find yourself doing this sort of code, then you don't necessarily have to use auto layout.
I should acknowledge that there are some tasks that are trivial in a non-autolayout environment (e.g. animation of moving views), doing the same task in auto layout can be frustratingly complicated. But if you have complex interdependencies between views (either between the superview and its subviews or between sibling views), auto layout can be useful.
Autolayout simply provides rules for the views how they appear in all orientation.It is upto the user to define how to use it or you may follow the struct and spring model .
The major difference between the two is that there no dimensions involeve in the struct and springs while autolayout says the dimensions and values in whch views should be rearranged
An excellent beginner tutorial fo autolayout

Scrollwheel as UISlider replacement

How can I integrate a Scrollwheel into my application?
I'm currently using Sliders but have found them to be sometimes difficult to control exactly (for example with a linear scale from 0% to 100%). I guess they weren't designed for that purpose and are meant to be used for cases where not pitch perfect control is ok (Volume Control and the likes). However, I really need an exact way of inputting data (other than TextFields, they won't work in my case).
I figured that a Scrollwheel kind of UI Element would be perfect for me. Are there any opensourced Scrollwheels available that would fit my needs?
Horizontal, just like Sliders
Variable Start and End Values
Variable Scale
Small in height
Pretty :)
I tried using the Picker but that didn't work for me since it shows it's values inside of it, which makes it both big and not pretty to look at when used multiple times inside of one View.
If there's nothing available that fulfills my needs (described above) could someone please give me a hint on how to start effectively with creating such a UI element? Thanks!
I've finally found something which fits my needs :)
OBSlider, a subclass of UISlider which allows variable scrubbing speeds – it imitates the behavior seen while scrubbing in iPod.app.
Fulfills all my needs:
Horizontal
Variable Start and End Values
Variable Scale
Small in height
Pretty :)