I am having a very hard time understanding that when I place a UILabel in interface builder and then get the coordinates in my code like this:
self.myLabel.frame.origin.x
I always get 0. Why is that even though I have placed the UILabel on the center of the screen? How can I get the correct coordinates?
The UILabel is being added to a UIScrollView. The UIScrollView covers the whole iPhone screen 320X480. UIScrollView is added through interface builder.
NSLog(#"%f",self.scrollView.bounds.size.height); returns 0
The origin is always relative to the parent view, in your case the UIScrollView. The way a UIScrollView works is that its contents can have a fixed origin but still appear in different places as the content size and offset (scroll position) are changed.
Furthermore, you did not say anything about the size of your UILabel, so it's also possible that the text within is centered but the view itself is much larger.
Also, you don't say where in your code you are checking the coordinates; if you check them too soon they may not yet be set.
Perhaps you are checking it before the label has been added to the view. Try putting a breakpoint somewhere in your code when you can actually see the label on screen and checking the values then.
Are you sure that self.myLabel is not nil, and that you've connected it correctly to your code? Also that you are loading the view from a xib file and not just using alloc/init on a view...
If non of the previous answers solved your problem,
Make sure you connect the label from the interface builder with its IBOutlet in the interface header file in your case the header should contain
#property (nonatomic,weak) IBOutlet UIlabel* myLabel;
and connected from the interface connector correctly with this property
Related
Possible a duplicate, but I tried could not make it work so came here.
I (new to autolayout) have two UILabel place one below each other with fixed height space.Both can increase with as per text with in it.
When First UILabel hides bottom should move to First place. How to do it using constraints in view only? I know how to do by creating IBOutlet connection of constraints for second UILabel.
EDIT:
Given question is about more about content hugging related, where as my question is add constrains to move to first UILabel position when first hides.
The only way to achieve what you want with AutoLayout is to set constant of all related constraints to zero. And remember NEVER try to set frame or bounds of your view if you are using AutoLayout(unless you override layoutSubviews and do the stuff in that method, which you rarely need to).
You can check out this tiny project: https://github.com/neevek/UIView-Visibility, I bet that is what you want :-)
I'm trying to adjust the size of a button to (it's intrinsic size + a little bit more) in order to draw a custom background. However, every time I access self.titleLabel within the button, the size and position resnaps to that of the storyboard. I don't have to do anything with the label to reproduce this, just retrieve it from the button.
I've put logging code all over my button and view controller in order to find where this is happening. It's not coming from a relaying-out of subviews or any other notification I see to get within the view controller. The line before accessing titleLabel, the position and size are correct. The line after, it has snapped back to the storyboard position. Commenting out the access prevents the size/position snapping. Can someone tell me where or why this is happening?
I have no constraints set (that I can tell), but am I fighting against auto-layout here? Should I be doing this a different way like composing controls or something?
Something similar (or the same?) has been asked before at UIButton modifying titlelabel seems to change its frame and UIButton titleLabel resizes on press?, but both were left unanswered or explained away with just "maybe a bug."
If the project has auto-layout enabled, then YES, you're fighting auto-layout. You have two choices, either subclass UIButton so that you can override the intrinsic size calculation, or modify the constraints so that the intrinsic size is not used in any constraint. If you do the latter, then you probably want to create an IBOutlet to the constraint for the width, so that you can adjust the constant property as needed.
This isn't a bug, it's a consequence of auto layout. When using auto layout, you shouldn't set any frames. Instead, you should change the size or position by modifying the constraints. What's happening, is that whenever the view needs to be redrawn, the frame reverts to the frame that's defined by the constraints.
I an making an extremely simple scrollview and doing it entirely in Interface Builder and cannot get it working.
According to IB the height of my ScrollView is 568. I have set a user defined runtime attribute called contentSize and set it at 320x900. Yet it simply will not scroll.
Here's a screenshot of my view:
If it was working, the word label should be bouncing all around, am I correct? Nothing happens at all. This is done entirely in IB I haven't touched code yet for this.
You need to uncheck the AutoLayout property in IB
You can refer this.
I have a weird behaviour here : my UIScrollView only want to work when I had at least 1 object from Interface Builder.
Because I also add some subviews dynamically like :
[scrollView addSubview:player.view];
But it doesn't want to work except if I drop at least 1 item from IB into scrollView.
Note that to declare my scrollView in my header file I drag and dropped using IB and i created an IBOutlet :
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Then I synthesize it.
Anyone experienced the same issue ?
Thanks for help and ideas.
As far as i know, you need to write at least a little code for the UIScrollView to work, at least contentSize must be set.
Add another IBOutlet for your content (eg. scrollContent) in the scrollview and set the scrollview contentsize to the bounds of the scrollContent in viewDidAppear
Try debugging your code. Probably your contentSize is zero in viewDidLoad. Thats normal, the size of the view is only know at viewDidAppear.
For sample code, see http://bitbucket.org/robvanderveer/scrollviewdemo.
I am not sure what is going on and how to handle this. I have a UIView in a cell and I set the constraints using IB and for some reason it stretches out to the end of the cell. How can I stop it from stretching.
And here is the result:
I am dynamically injecting a stepper control but as you can see the UIView stretches to the end of the cell. I tried pin width then it completely messes up everything and does not even display the UIView.
Here is the screenshot of constraints:
Okay, let me draw some distinctions here.
The trivial way to this is you always have the stepper in the cell (and no UIView). For those cells where we are not supposed to see the stepper, you set its hidden to YES; for those cells where we are supposed to see it, you set its hidden to NO. That is what I would do.
The programmer way to do this is you don't have any blue UIView in your cell at all. You add the stepper in code, and you also provide its constraints in code to position it. You said:
so I don't have to hardcode the coordinates
But adding constraints in code is not "hard coding" the coordinates. With constraints, you describe the position conceptually in terms of insets from the superview, and as the superview resizes, you are still correctly positioned. So there is no philosophical objection to adding the stepper and its constraints, and it is just laziness not to do that.
The lazy but wasteful way is to do what you are doing, i.e. add the blue UIView in the nib as a guide. Even if you do use the blue UIView as a guide, though, you should be able to get this right if your constraints are right. I just tried this and I was not able to reproduce the problem: my blue view appeared in my cells in the same place where it appears in Interface Builder.
The instant solution is don't use Autolayout in the nib from which a UITableViewCell is drawn — i.e. uncheck "Use autolayout" in the file inspector for this nib. You will thus use old-fashioned springs-and-struts (autoresizing) to position the subviews.