Fixing two uiviews size based on screen width & height in storyboard IOS 7 - objective-c

Please tell me: how to do the below logic in storyboard using constraints.
Fixing two UIViews(minimum :130x80) in screen. The center gap between uiviews always 20px.
screen:
20px View1 20px View2 20px
Thanks in advance!

Add the Leading Space Constraint between view1 and the screen : 20px
Add the Trainling Space Constraint between view2 and the screen : 20px
Add the Horizontal Spacing between the views : 20px
Add the Equal Widths between the two views.
And to not have errors, add the top + bottom constraints, or the Vertical center + Height on view1
Finish by adding the Same Top + Same Bottom constrains on view2.
Enjoy ;)
EDIT :
Huh, I forgot that your views have Width >= 130.
Then, 130 + 130 + 20*3 = 320, which is the width of the screen on iPhones <= 5S.
And when you use the leading/trailing space constraints, iOS will start to count from 16px, then Leading Space 20 will give you X = 36, then you'll miss some place.
The solution is to set X = 20, and then pin the Leading space, or modify Leading Space value from 20 to 20 - 16 = 4. Same thing for the Trailing Space of the View2

Related

Embedded .xib appearing in wrong position

I was hoping this was an iOS 8 bug (it still might be; everything works fine in iOS 7) but I'm still seeing it in the iOS 8 GM Seed.
I have a CategoriesView.xib embedded in DetailView.xib. When the DetailView.xib is displayed, the placeholder UIView displays in the right location, but the contents of CategoriesView.xib appear exactly 72px above where they're supposed to. I'm using Autolayout. The 72px seems to be 20px status bar + 44px navigation bar + 8px auto layout constraint from the top of the image.
Inside CategoriesView.xib, there are actually 5 little icons representing each possible category. Each icon has a width/height constraint and a leading space constraint to add padding from its neighbor. Typically a screen will only show 1-2 categories, and I accomplish this with this code:
// all width constraints are part of the categoryViewsWidthConstraints Outlet Collection
// all padding constraints are part of the categoryViewsSpacingConstraints Outlet Collection
// loop through my outlet collection, and set all width and padding constraints to 0
for (int i = 0; i < self.categoryViewsWidthConstraints.count; i++) {
[self.categoryViewsWidthConstraints[i] setConstant:0.0];
[self.categoryViewsSpacingConstraints[i] setConstant:0.0];
}
// now, for the categories that need to show, add the width and padding constraints back
for (int i = 0; i < self.categoryIds.count; i++) {
NSNumber *categoryId = self.categoryIds [i];
[self.categoryViewsWidthConstraints[categoryId.integerValue - 1] setConstant:20.0];
[self.categoryViewsSpacingConstraints[categoryId.integerValue - 1] setConstant:8.0];
}
Again, this works fine in iOS 7.
I thought I had this working when, inside CategoriesView.m, I set:
self.contentView.translatesAutoResizingMaskintoConstraints = true.
That gives me the correct vertical placement I'm after, but unfortunately I get auto layout errors because it thinks CategoriesView needs to be exactly 132px wide. Because of this, the last category icon gets stretched:
I'm at a loss here. I know this is pretty nuanced, but any ideas?
I tried everything I could think of, and just could not get this to work.
My solution? I simply created a brand new file and reconstructed all the elements and all the constraints. And it worked perfectly.
So so frustrating, but things are finally back to normal. I think it had something to do with the transition from Xcode 5 to Xcode 6.

character is of string is always changing the position when ever I change the string value by using CCLabelBMFont

I update the score label when I'm playing my game and I display it by using CCLabelBMFont, when the score changes, each character of the score label always change the position of the label.
I want to keep it the same position!
Example: 00:01 take small space than 00:50; when the text change the CCLabelBMFont reposition the text again. help me , how to keep the same position?
Your bitmap font was created from a true type font that has a variable width.
Only fixed-width fonts like Courier will give you the same string width for the same amount of characters in the string. Strings drawn with non-fixed-width fonts will have varying width depending on the characters in the font, ie a 'w' character has greater width than an 'i' character.
Open FontBook.app (it comes with every Mac) and in the Collection panel select "Fixed Width" to see only the fonts with a fixed width. On my system I have these fixed width fonts installed:
FYI, Monaco is the default font used by Xcode.
It is hard to tell by the question, but my guess is that you are talking about the actual positioning of your label changing, meaning your alignment is not what you want it to be (justified left instead of justified right for instance).
Left Justified:
test
testtest
Right Justified:
test
testtest
If this is what you are talking about, you can change the text alignment by changing the nodes anchor point.
// for right justified
label.anchorPoint = ccp(1, 0);
When you are saying "always changing the position", do you mean "left side isn't staying in the same place"?
CCNode class has anchorPoint property, which determines which point of this node is "pinned" to the point in node's parent. Anchor point of (0, 0) means bottom left corner, (1, 1) - top right, (0.5, 0.5) - center. All positioning and transformations take place around anchor point.
Set anchorPoint property of your label to mean the point which you want to have "pinned" when content size of label changes. Examples:
if you want top left corner to be stationary:
label.anchorPoint = ccp(0, 1);
if you want the middle of left side to be stationary:
label.anchorPoint = ccp(0, 0.5);

Using iOS 6 Auto Layout to position view relative to size of superview

I wish to have objects positioned relative to the size of the superview instead of absolutely.
For example, I do not want to say that an element is "50px from the top", I want to say it is "1/3 of the way down".
Is there any way to do this using auto layout? If not, what is the best way?
Cheers
Add an Outlet to the height constraint of superivew and an outlet constraint to the topspace. In code set the constant of topspace constraint to a value equal to 0.33 * height constraint of superview.
If you use IB, just place the element where you want it and set it to maintain its Top Space to Superview. This will hold the element at that distance from the top for both 4 and 3.5 inch devices.

UIScrollView, how to set size to image size?

I have UIScrollView, with an UIImageView on top. I have a couple things I am trying to fix:
1) Initially, the image is scaled to fit in the screen (this is good). How do I set a boundary so that you can not zoom out past this point (I don't want to see any of the background)?
I looked around for some sort of maxSize property but couldn't find anything.
2) How do I set the amount that someone can zoom in? I don't want users to be able to x8 or anything like that.
1 )
[scrollview setMinimumZoomScale : float];
2 )
[scrollview setMaximumZoomScale : float];

Objective C: Change Line Width Using UISlider

I have this code to change the stroke width of the line i'm using to draw on the screen.
- (IBAction)changeSize:(id)sender
{
if (Slider.value > 25.0)
{
Width += 3;
}
}
It's working but when i drag the slider thumb straight to the maximum it just adds a little size only but when i do it partly it's getting bigger each step.
Also, when i decrease it on the UISlider it still adds more... it's not decreasing...
How can i do resizing using UISlider??
It looks like you are telling it to increase the width by 3 when the slider is changed.
You would need to set the width equal to the slider value.
Well, you need to understand how sliders work. The Slider.value is your current slider value and it goes from Slider.minimumValue to Slider.maximumValue, witch you can set when creating the slider. You should set those to your minimum line stroke and to maximum line stroke.
In your changeSize method you should then only have 1 line: Width = Slider.value; What you are doing now, is that each time you move the slider and its value is larger then 25.0, you increase the stroke by 3.
As for the speed of slider movement effecting the stroke in your case: How many times the method changeSize will be called is time based, not value. So if you slide it faster, it will be called lesser times then if you slide it slowly, therefor increasing your Width lesser times by 3.