Got a map which is composed of multiple parts:
Each part is an UIButton with an Image. I positioned everything to make up the map and it first looks all right. The problem I have is about resizing the View for different screens sizes. Tried with Autoresizing and Constraints but couldn't get it to look properly for minimizing and maximizing it. The parts should always sick together and up-/downscale according to the screen size. I'm testing the behaviour directly in the Interface Builder by resizing xib's dimensions.
What works is setting all arrows in autoresizingMask for each UIButton and maximizing the view. But minimizing it distorts the map.
Unfortunately it's the first time for me working with the Interface Builder in Xcode. Here you find the source files. Thanks!
Edit:
Setting only the arrows for width and height in the autoresizingMask makes it possible to downscale without distortion. Upscaling pulls the parts apart. How to support proper up-/downscaling simultaneously?
Edit1:
Replaced the every UIButton with UIImage which resize properly in the Interface Builder! See repo here. Unfortunately on simualtor devices the Autoresizing differs and the map-parts don't stick together anymore. Setting the size of UIView in Main.storyboard exactly to xib's size the Autoresizing works perfectly (tested on multiple devices). But later on in the "real" project I'm not in control over the parent's view dimensions.
Played a litte bit with this line of code and it seems to be responsible for "blocking" Autoresizing. How to avoid that? Thanks in advance. I think I’m maybe mixing up Autoresizing with AspectFit which seems to have no effect.
I guess it would be much easier if all the Bundesländer images had the same size. Put them all on top of each other in a common view. Give all the buttons zero padding to all side. Subclass UIButton to check if the the touch event is on a non transparent pixel. Search for "irregular button ios" to find out more about it.
I am able to achieve this using this images, which have all the same size, border added to demonstrate size
and this constraints
The most important constraint might be the Aspect Ratio Constraint on MapWrapper, the view that contains all the buttons.
Using Ole Begemann's OBShapedButton
Some notes:
Your image content is perfect for factorised graphics. Export them to pdf and add them as such as single scale to your assets
you should not include the states names' in the images, but use it as the buttons titles'. To position them correctly, use title insets.
example project: https://github.com/vikingosegundo/germanStateSelection
Related
I've designed a ViewController with an ImageView, 3 Label-TextField combination, one single Label and one Toolbar at the bottom of the view. All of these view elements are inside a Stack View. The design is like below:
The contentMode property of the ImageView is set to Aspect Fit in the Attributes Inspector.
Now when I run the app and select different size of photos, the ImageView seems to be inconsistent different times. Like:
The teal color background the ImageView is set to distinguish the difference between the ImageView and the original Background.
This is okay. But when I choose a large size photo it seems to be inconsistent as the ImageView is overlapping the Toolbar below and this time the Camera button is no more clickable. Here is the image:
And the design gets more broken when I choose another photo. The view is showing up differently in different times. Screenshots are: and
Now what to do? I haven't done any of these design from the code side, all from the Interface Builder.
Any help will be appreciable.
I have these options in the Drawing section of Attributes Inspector:
And this is my Stack View's attributes:
Well it seems everything is fine. Just add height constraint in your Toolbar. The problem will be fixed.
First, make sure you've checked Clip To Bounds property of UIImageView in Attributes Inspector.
Second, set your constraints and / or Stack View's Distribution property in such way that all your views fits within available height.
Here's a sample: https://www.dropbox.com/s/5a7r0oz1v8vclgz/AspectFitImageView.zip?dl=0
By searching a lot of ways around, I finally was able to sort out the issue. Setting the content hugging priority(don't want to grow) and content compression resistance priority(don't want to shrink) helps a lot. Basically I needed to adjust the elements vertically. So tuning the vertical priorities for each element solved my problem. As I have to keep the size of the ImageView to be changed from time to time I've made it's vertical hugging and compression to lower than the other elements of the Stack View. Other priorities are kept as they were by default.
To visualise the situation, here is attached images. Color combinations are used here to distinguish among the elements of the Stack View.
Hope this helps.
I have a really stupid question...
I'm afraid I'm stuck at the basics, trying to fit a 2732 × 2048 (Read up that this is the resolution for the iPad pro, for which I'm building an app) image, but it doesn't scale no matter what I do. I want it to scale to fit the screen, rather than be beyond the borders of the screen.
I tried playing with the align constraints, but nothing changed no matter what I adjusted...
Thanks in advance!
Edit: Added an image and described my failure
drag a UIImageView to the controller in Storyboard.
scale it to match the size of view in controller, add some constraints by tap Editor in menu bar -> Resolve Auto Layout Issues -> Reset to Suggested Constraints.
Set image of UIImageView to you image, set view mode to Scale to Fit
I am not familiar with Auto Layout, this may not be the perfect way.
Im starting to experiment with storyboard. As you can see I have chosen the storyboard size to be height=regular and width=compact, which says “For all iPhones in portrait”.
The simulator I am using is iPhone6 however when I run everything is slightly off to the right.
Can someone explain what is happening or what I am missing?
The problem is that (as described in the comments) you are positioning the views without AutoLayout. When you just drag and drop the views, it's actually setting the frame's positions and sizes. The main problem with this approach is that it doesn't set the position and size in a proportional and related manner, taking in consideration the container. This is why you're getting the view at the current position. If you run in another simulator, maybe you can get the correct position, or maybe not. What you can do to change this is apply auto layout constraints to those views. There's a special constraint to center views horizontally. With autoLayout you can go further, specifying relation between one or more views, and those views not necessarily need have the same container view.
One quick example:
Notes:
I'm using universal storyboard to take those screenshots. It's more flexible and with AL you don't necessarily have to concern yourself with the size, as views adjust themselves depending on the constraints applied to.
EDIT:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html
This is a link to auto layout guide provided by apple, a great tutorial on how to get your hands dirty in this little world.
I'm new to iOS development and I'm working through my first tutorial. One thing the tutorial kind of skipped over is positioning. I have a list where each cell has an image with some text next to it. At least in Interface Builder that's how it appears. When I actually run the app the image appears on top of the text (i.e. both have the same exact positioning from the left-hand side of the screen).
Interface Builder:
Running App:
I essentially want what, in CSS lingo, would be "float: left", or even just relative positioning with x/y coordinates for each element (i.e. if it would look the same/similar when running the app as it appears in the storyboard), that would be great.
In playing with all of various View attributes, I'm not finding anything that will position the views… I can find attributes that will position them in Interface Builder, but none of those settings seem to carry over to the app when it is running, and the image/text always appear on top of each other.
I apologize for the newbie question… it's probably something simple/obvious that has somehow slipped by me.
If you are using Xcode5 to do the test, the app will use AutoLayout feature by default. And the constraint you made for this layout seems to be wrong.If you want to position the views to the right place with AutoLayout ,you need to read some material from apple official website.If you do not have too much time to learn,you can try to edit xib without AutoLayout. You can disable AutoLayout in "file inspector" of interface builder.
If you are new to iOS development (as I am), you should familiarize yourself with AutoLayout, as suggested in Eric's answer.
If you're looking for a quick fix to the question as asked, all of the following options worked for me:
Select the label, then select the Horizontal Space Constraint and delete it.
Select the label and hit cmd-plus (i.e. cmd+shift+equals). This will delete all of the positioning constraints at once.
Turn off AutoLayout.
There are other ways to fix the problem I was having, but generally the issue was incorrect AutoLayout constraints.
If you feel comfortable in the code, I suggest manually setting the frame to a CGRect that you define with CGRectMake(origin.x, origin.y, width, height). You can also do this with the autolayout settings but I find that programmatically positioning elements gives me much more flexibility, and, as a programmer, it's much easier than trying to figure out the options in interface builder.
The iOS coordinate system is top-down, meaning that y=0 is the top of the screen and y=568 is the bottom of the screen (on iPhone 5). X is 0 on the left side of the screen.
Coordinate systems are relative to the view that they are in. In your case, your elements are in a UITableCell, which is given its own coordinate system by the rendering engine. So if you want your image to be 10px from the top and 10px from the left of the cell's edge (a la padding:10px in css), you would set its frame like so:
image.frame = CGRectMake(10, 10, 50, 50)
...assuming the image is 50x50.
I have so far developed for Android. There, you can set layoutMargin and padding for almost every UI element (when you describe UI in xml file).
How is this done in code for UIViews in iOS (if there is a common command)?
Tnx
iOS employs a different approach to control placement - there's no layouting engine, you specify absolute coordinates and size for all controls. Kinda like the AbsoluteLayout in Android.
So the concepts of margin and padding don't really apply - the gaps between adjacent controls are completely up to you, they're not computed by the system. Same for sizes.
This makes it more difficult to implement the scenario of "make this control as large as it needs to be for its text". However, you won't run into rogue line wraps.
EDIT: AbsoluteLayout is deprecated these days.
You lay out your UI elements either in code or using Interface Builder (or the .xib editor in Xcode 4). You'll specify actual coordinates for your views, but you can also specify the resizing behavior for each view. So, you can say that a particular view should always keep the same size and remain horizontally centered, or that it should keep it's size and maintain the margin on the left, or that it should stretch to keep both left and right margins, etc. Look for the "autosizing" section in IB, or set the autoresizingMask property of any view in code.
In ios you can use Interface Builder for design. You can still use xml, but no one use this.