I place a UIProgressView on an XIB file. At first it goes well. After when I set its height via auto layout to be 20, problem occurred. The Progress View always got its position at the most top-left of the screen. even I use
[self.progress setFrame:CGRectMake(93, 100, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
still don't work for me at all.
Thank you very much for any help.
use size constraints as outlets. Then you can easy change values of those constraints. It's not really recommended to modify frame when using auto-layout.
declare a property:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;
and in the implementation:
self.heightConstraint = self.scrollView.frame.size.height;
self.widthConstraint= self.scrollView.frame.size.width;
Alternatively you can try to set frame in viewDidLayoutSubviews but it's not recommended when using auto-layout.
Related
I have created the IBOutlet for the constraint of top spacing . I need to update the value that constraint programatically in viewDidLoad. Here is my declaration in IBOutlet :
IBOutlet NSLayoutConstraint *labelTopSpace;
and here is how I changing the top:
labelTopSpace.constant = 50.0;
but this is not working in my case . am I missing anything ?
Update : should I make the property of it ?
Hope, you have mentioned all the constraints under method!
- (void)updateViewConstraints
{
[super updateViewConstraints];
_labelTopSpace.constant = 50;
}
Try using below code..
[btn setConstraintConstant:50.0 forAttribute:NSLayoutAttributeTop];
you can replace the btn with any Ref which for which you have that Constraint that need to modified.{say self.view//if it is for view}
In this case you no need to create property or IBOulet reference to your Constraint..
You can Directly change as above..
Hope it is useful to you as alternate solution...!
Make it property, like
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelTopSpace;
then check outlet connection. similarly asked question Unable to make outlet connection to a constraint in IB
I am designed my screen in xib using autolayout(xcode 6). I have to redesign the screen programmatically using autolayout.
I have 4 views in my screen horizontally. I have to hide the third view and move the fourth view to third view's position using autolayout.
How do I update the constraint programmatically.
You may use NSLayoutConstraint and assign proper IBOutlet to any of your constraints and then where you wants to change the view size use constant property of NSLayoutConstraint to change the value.
e.g
in your viewController.h
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *tempConstraint;
some where in your viewController.m
tempConstraint.constant = 50; // or what ever you want
I want to change position of UITableview position programmatically. I have added following code in viewDidLoad,ViewWillAppear callbacks, but position is not changing.
Table.frame = CGRectMake(0,100,320,200);
When I disable Auto Layout option in Storyboard, Table View's position is changing. But, I need to keep Auto Layout option to maintain support for different device type.
Use autolayout, and set constraints to your tableView's position & size.
Then, create IBOutlet instances of the type NSLayoutConstraint, and attach them to your constraints in the xib.
Then, in your code, set the values of the constraints to reset the table view position.
Sample code:
in your .h file:
#property IBOutlet NSLayoutConstraint *tableOriginX;
#property IBOutlet NSLayoutConstraint *tableOriginY;
#property IBOutlet NSLayoutConstraint *tableWidth;
#property IBOutlet NSLayoutConstraint *tableHeight;
Then, in your .m file where you want to alter your table's frame:
tableOriginX.constant = 0;
tableOriginY.constant = 100;
tableWidth.constant = 320;
tableHeight.constant = 200;
I have two view controllers inside a tab bar navigation. Inside the second scene I have an additional view (just a simple UIView) and a button to set it's color and bounds.
CGRect viewRect = CGRectMake(20, 20, 70, 70);
self.animationView.bounds = viewRect;
self.animationView.backgroundColor =
[UIColor yellowColor];
This code works fine. But if I navigate to the first view controller and then back to the second view controller my view is still yellow but it is back at the size and position I set in interface builder.
How can I prevent this?
This behavior ends if I disable autolayout but I don't really want to do that.
Create outlets for the animationView constraints, and change their constant value.
In the .h file of the viewcontroller:
Connect the outlets to the correct constraint in the IB:
//AnimationView Height Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cHeight;
//AnimationView Width Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cWidth;
//AnimationView Leading Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cLeading;
//AnimationView Top Constraint
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *cTop;
In the .m file of the ViewController set the constant value of the constraints, instead of the frame:
- (IBAction)btnTouched:(id)sender {
[_cHeight setConstant:70];
[_cWidth setConstant:70];
[_cTop setConstant:20];
[_cLeading setConstant:20];
self.animationView.backgroundColor = [UIColor yellowColor];
}
It'll work fine.
I have created a table view with prototype cells in a Storyboard with the "Use Autolayout" unchecked in Xcode 5.
The cells are UITableViewCell subclasses, mainly to add IBOutlets and no code inside. The result is a messed layout. I tried changing the Autoresizing masks with no luck. Also tried this.
If I however implement an empty layoutSubviews it shows ok. Any idea of what's going on?
Is auto layout still enabled despite unchecking it?
Edit:
More details...
#interface SettingDefaultTableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *label;
#property (nonatomic, weak) IBOutlet UIImageView *imageView;
#property (nonatomic, weak) IBOutlet UIView *backgroundView;
#end
#implementation SettingDefaultTableViewCell
- (void)layoutSubviews
{
// Emtpy implementation to fix weird layout bug
}
#end
Storyboard with Autolayout disabled:
Result when layoutSubviews fix above is not used:
Edit 2:
The cell is set to not autoresize subviews.
While the above should be enough to prevent subviews from getting auto-adjusted, also all autoresizing masks are set to flexible right and bottom margin only.
Only the > mark is set to flexible left margin. No flexible width or height.
The cell is getting modified by the undocumented UITableViewCell's layoutSubviews:
Despite set not no autoresize subviews, the default implementation changes the backgroundView frame.
It also moves imageView (the ">") to the left despite being configured as a Custom Style cell.
Thus avoiding executing super's layoutSubviews fixes it.