Depending on the height of the screen, I would like to adjust the height of a button in a view. What is the easiest way to do this in Swift?
I tried it in this way and also with CGRectMake() but nothing changed:
self.myButton.frame.size.height = self.myButton.frame.size.height*scrOpt
How can I "update" the frame?
The reason why you see no changes may be because you're using AutoLayout, and the button has some constraints applied to it, and you need to change the height constraint to accomplish what you want.
Edited: Changing frame properties directly seems to be possible in Swift but was not possible in Objective C.
If you are using auto layout, you need to update its height constraint, else update its frame
NSLog(#"%#",NSStringFromCGRect(self.myButton.frame));
NSLog(#"%f",scrOpt);
self.myButton.frame = CGRectMake(self.myButton.frame.origin.x, self.myButton.frame.origin.y, self.myButton.frame.size.width, self.myButton.frame.size.height*scrOpt)
NSLog(#"%#",NSStringFromCGRect(self.myButton.frame));
Edited check this and see what is Print NSLog
Related
I have a need to set the contentSize of a non-scrolling UITextView exactly to it's superview's frame. I need to do this for the purpose of getting the range of characters that fit, and while I know there's better methods for doing this, or even better views to be using, I need to use this custom UITextView for it's specific capabilities. Simply doing:
textView?.contentSize = superView.frame.size
doesn't seem to set anything, and it's contentSize is still as long as it's content. I want to simply truncate the content at where it just wont fit anymore. I'm on Swift 3 and xCode 8 if that makes a difference.
You can do so using .bounds:
textView?.bounds = superView.bounds
That should set all the properties you want to be the same as the super view.
Figured it out. Scrolling was enabled. Disabling scrolling seems to have fixed it.
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 want to set a position of an object programmatically. For this I do:
youtubeIco.frame.origin.y = 100
but nothing. It doesn't change coordinates at all. What is the problem?
And a second question:
when I set in the XCode constraints of webView(for ex: 10(top, left, right, bottom) it disappears. But when I do not set constraints for it, it appears. What is the problem?
I want like this:
You need to set full frame like,
youtubeIco.frame = CGRectMake(youtubeIco.frame.origin.x, 100,youtubeIco.frame.size.width,youtubeIco.frame.size.height)
Only y-axis change will not do anything to it, you have to provide full frame for that. or set centre, or offset.
With AutoLayout you can't change a view's frame directly. The constraint puts the object right back where it was.
Instead, you should add constraints that position your views and add IBOutlets to those constraints. Then you use the outlet(s) to change the constant value of the constraint, and the constraint moves your view objects for you. (In this context anything you can display on the screen is a view {a descendant of UIView} including buttons.)
I want to change the height according to content of cell. I don't want to calculate height every time for each text. This should also work for any with of text means label should update on its orientation change also.
Is there any feasible way to do this?
Please refer link below. This is the best way to do exactly what you want.
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
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.