I need to update UIView size.
Now i use
var titleSize = CGRectMake(0, 30, 40 , 1000)
self.contentCoverScrollView.frame = titleSize
in viewDidLoad, but nothing happens.
In Autolayout if you want to change the frame then try the constraints (IBOutlet of NSLayoutConstraint).
Set the constraint outlets and change constant value by :
self.sampleConstraint.constant = 20
self.view.layoutIfNeeded()
I thought you had used the AutoLayout in your application that's why it can not change the view frame, please write the below code.
var titleSize = CGRectMake(0, 30, 40 , 1000)
self.contentCoverScrollView.frame = titleSize
[self.contentCoverScrollView updateConstraintsIfNeeded];
Note : updateConstraintsIfNeeded will tell the system to recalculate the constraints in that particular moment of execution.
Look like you want to change ScrollView size use:
[self.contentCoverScrollView setContentSize:(CGSizeMake(40, 1000))];
Related
Is there anyway to set the height of a UILabel programmatically? I've added a bunch of constraints to my .Xib files so every other label is dependent upon the one above or below it for it's positioning. It'd make my life so much easier if I could just use:
nameLabel.height = 0
My .Xib looks like this:
plz take the IBoutlet of constraint height and set the height you want
try this:
var frame: CGRect = nameLabel.frame
frame.size.height = height
nameLabel.frame = frame
Write this line :
constLblHeight.constant = 0;
//Below image step
//Step 1: Add Constraint in label height
//Step 2: Create object of label height constraint (constLblHeight)
If you empty the label its height will be zero.
nameLabel.text = ""
Even no need to hide it.
I have a UIView called descriptionView and I want to hide it initially when the screen first loads by offsetting the y coordinate to be the screen size + the height of descriptionView itself:
However, in my controller, none of my frame changes do anything:
override func viewDidLoad() {
super.viewDidLoad()
...
// descriptionView.frame.origin.y = self.view.frame.height
// UIView.animateWithDuration(1, animations: {
// self.descriptionView.frame.origin.y = self.view.frame.height
// self.view.layoutIfNeeded()
// })
//
print("xxx")
descriptionView.frame = CGRectMake(0, self.view.frame.height, self.view.frame.width, 66)
// descriptionView.frame = CGRectOffset(descriptionView.frame, 0, descriptionView.frame.height)
}
No matter what I do it seems fixed at that visible position like in my storyboard. Can someone help?
In IB you are using NSAutoLayout, so you either need to manipulate the constraints, or tell the view to translate the mask to constraints.
If you want to set the frame directly then you will want to do this:
descriptionView.translatesAutoresizingMaskIntoConstraints = true
descriptionView.frame = CGRectMake(...)
Otherwise you can create IBOutlets to the height and width constraint from IB and update those:
self.descriptionViewHeight.constant = self.view.frame.width
Additionally, I would recommend doing frame manipulations inside of viewWillAppear: rather than viewDidLoad. viewDidLoad does not strictly guarantee final position.
Instead of editing the frame or descriptionView, edit its height constraint.
First, create an NSLayoutConstraint from this constraint by cmd-dragging the height constraint from the Interface Builder to your class (like you do for any UI object).
Then you can set the constant property of this constraint to 0.
(Yes, constant is declared as a varproperty...)
If you are using constraints, chaging the frame view myView.frame property will not affect on view actual position and size. Instead of this make constraint outlet and change it in your code, it will look like this:
descriptionView.heightConstraint.constant = 100
Also, if you want to hide something, you can use property myView.hidden = true, or myView.alpha = 0;
I feel like this is a softball for you veterans, but I'm having a lot of trouble resizing the subview of my UIImageView... here is what I got:
var myImageView:UIImageView!
var tmpView:UIImageView!
myImageView is my "main" UIImageView, and I'm treating tmpView as a subview... now I've tried both with and without auto layout on, but I've set the constraints of myImageView s.t. myImageView takes up the whole screen. I've confirmed this to be true by setting myImageView.image = UIImage...etc.
Here is my code for initializing the image and adding it to the subview:
self.tmpView.image = UIImage(named: "myImage.png")
self.tmpView.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
self.imageView2.addSubview(self.tmpView)
Now here is where I am running into problems - no matter what I set tmpView's height and width to, the size never changes. Interestingly though, changing x,y does have an effect on the position of the subview.
Here are my questions: 1) Why do both x and y obey nicely, but width and height do not?
2) How do I fix this, and is it best to do so programmatically or through the storyboard?
You should no longer modify the frames directly when using autolayout. Add a width and height constraint to tmpView, and create outlets for them if you're using IB (The outlets should have the type NSLayoutContraint).
You can also create the constraints in code but there's no point of doing it that way unless you have to.
In both cases, to resize tmpView, change the constant property of each of the constraints to the values you want.
widthConstraint.constant = yourNewWidth;
heightConstraint.constant = yourNewHeight;
How can I position a UIButton right at the end of a dynamic UILabel?
Here is a screenshot of what I'm trying to achieve:
Are you using Autolayout? If so, add horizontal spacing constraint and baseline alignment constraint between the label and the button. Make sure also that numbers of lines of the label is 0.
Try
[your_label sizeToFit];
CGRect rect = your_button.frame;
rect.origin.x = CGRectGetMaxX(your_label.frame);
your_button.frame = rect;
I'd override -[UIView layoutSubviews] and do what you want there. Once you do that, the code will look something like Avt's answer. If you want the label to have multiple lines, but a constrained width, you may want to try something like
CGSize labelSize = [yourLabel sizeThatFits:CGSizeMake(someWidth, 9999)];
and use that to set the label's frame.
If you want the button to appear at the bottom of the UILabel, use constrains.
I'm using autolayout on iOS7 and I have a problem like this:
I'm putting a UILabel onto a UIView and I'm arranging my autolayout constraints so that the label's centerX = parent view's centerX. I'm not giving any width constraint to the label. When I set the label's text on runtime, the label is drawn just wide enough for the text to fit, there are no margins/paddings on the left and right sides. What I want is to have some padding on the left and right sides, so that the text doesn't begin just where the label begins. The hack to achieve this could be setting the text as #" text " but of course that's not the way to go :)
How can I achieve what I want?
You can extend UILabel and override the intrinsicContentSize by yourself. Please make sure you have set the textAlignment = NSTextAlignmentCenter as well.
-(CGSize)intrinsicContentSize{
CGSize contentSize = [super intrinsicContentSize];
return CGSizeMake(contentSize.width + 50, contentSize.height);
}
Swift 5.0
open override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + 16, height: size.height)
}
This probably only works when you only have just one line of text to display.
You can create a UILabel subclass and override intrinsicContent,
-(CGSize)intrinsicContentSize {
CGSize s = [super intrinsicContentSize];
s = CGSizeMake(s.width + 20, s.height);
return s;
}
This will add a padding of 20 points to the width. If you want your text in the middle, be sure to set the text alignment to center.
If you're using auto layout, you can set the horizontal constraints and use an NSDictionary in the metrics parameter to set this dynamically.
For instance, if you wanted to give a 10pt padding to the inner content of a UIButton, you could do something like the following:
NSDictionary *padding = #{ #"padding" : #(button.intrinsicContentSize.width + 20) };
NSArray *buttonHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[button(==padding)]|" options:0 metrics:padding views:NSDictionaryOfVariableBindings(button)];
If you are trying to give the UILabel a different colour to its parent view, then you will need to enclose the UILabel in a UIView with the padding and the background colour you want.
If your UILabels background colour is the same as its parent view, then I don't understand the problem just use auto layout to specify how much space you want relative to the thing it is next to.