Why a frame of a UIView is not updating in ViewDidLayoutSubviews? - ios

I am trying to update the frame of a UIView which contains buttons and labels inside. I am trying to update it in viewDidLayoutSubviews (and I also tried in viewDidLoad, viewWillAppear, viewDidAppear..). I want to change the y position (origin.y) of the view.
The NSLogs says my original y position is 334, and after changing, it is 100. However, the position does not change in my view. I have already checked that the view is connected in the storyboard. What am I doing wrong?
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGRect theFrame = [self.bottomView frame];
NSLog(#"Y position bottomview: %f", self.bottomView.frame.origin.y);
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
// NSLog(#"iphone5");
}
else{
// NSLog(#"iphone4");
theFrame .origin.y = 100;
}
self.bottomView.frame = theFrame;
NSLog(#"Y position bottomview after changing it: %f", self.bottomView.frame.origin.y);
[self.view layoutIfNeeded];
}

I've had the same problem. Forcing the layouting for your view's superview helped me out:
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.bottomView.superview setNeedsLayout];
[self.bottomView.superview layoutIfNeeded];
// Now modify bottomView's frame here
}
In Swift:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
bottomView.superview!.setNeedsLayout()
bottomView.superview!.layoutIfNeeded()
// Now modify bottomView's frame here
}

Believe it or not the below code fixed it
override func viewDidLayoutSubviews() {
DispatchQueue.main.async {
// UI changes
}
}

The problem was related with Autolayout. However I couldn't turn it off since I am using autolayout in my project. I solved defining appropriate constraints in the view. Then there is no need to check if it is iPhone 4inch or 3.5inch and change the position of the frame since it automatically adapts to each size.

The frame setting should work in your code. But if the view has autolayout constraints (which I assume you have), your frame setting won't work. You can only go one way or the other (manual frame setting or autolayout), not both.

Related

iOS part of viewcontroller goes black when orientation changes

When I change viewcontroller orientation while it's loading on particular orientation, part of the screen goes blank at the bottom.
Steps to reproduce:
Have tableview(Not necessary I think. could be any view) in a view controller
Push a new viewcontroller which also has a tableview(Not necessary I think. could be any view) during
an action
Rotate the screen from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part.
Press back button to pop the current viewcontroller
Now rotate from one orientation to Another(Portrait to landscape or landscape to portrait).now you will be able to see the dark part here as well.
The bottom part of viewcontroller goes black.
I am able to reproduce 7 out of 10 times.
FYI:
My Viewcontroller has only tableview that's all and all cells has autolayout constraints.
Trying to understand why it happened and I would like to fix it.
More details on reproducing the issue:
Basically you can reproduce this issue only if you hold your device in slanting position and either push or pop a view controller and while the page is trying to load , you have to change the orientation then the issue happens.
(Pun intended.... lol )
In other words ,you have to tilt you device as if you are steering the wheel :).
This issue can only happen if you use your device like a steering wheel :)
In your viewWillTransition method call layoutIfNeeded()
Try to reset the layout of TableView after rotation by using this method,
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tableView.layoutIfNeeded()
}
or
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
}
I also experienced the same issue.
My view controller had just one tableview and when we tilt , I mean change the orientation during a navigational push or pop this used to happen.
Try setting the frame
in viewDidLayoutSubviews
-(void)viewDidLayoutSubviews{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
CGRect frame = self.tableView.frame;
// To check if there's any mismatch between the sizes of screen and tableview then set the tableview frame same as screen frame.
if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
self.tableView.frame = CGRectMake(0, 0, width, height);
}
}
or try setting the frame in viewWillTransitionToSize
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
CGRect frame = self.tableView.frame;
if (CGRectGetHeight(frame) != height && CGRectGetWidth(frame) != width) {
self.tableView.frame = CGRectMake(0, 0, width, height);
}
}
FYI:
if setting the frame self.tableView.frame = CGRectMake(0, 0, width, height); doesn't work then you could try [self.tableView layoutIfNeeded]
Set frame again in
viewDidLayoutSubviews
In one view controller(Parent the one that pushes new view controller) setting the frame at viewDidLayoutSubviews solves the issue in another view controller(The child view controller , one that's getting pushed) setting the frame at viewWillTransition fixes the problem :) But still I don't know why there's no universal solution to this problem.

view's frame don't change

I have a tableview.when reload data I will configure cell and update constraint. after that cell'slayoutSubviews be invoked,but view's frame can't be update
- (void)configModel:(LKSportActivityEventModel *)model {
//some code before
CGFloat titleWidth = 100;
[self.activityNameView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(titleWidth);
}];
//some code after
}
- (void)layoutSubviews {
[super layoutSubviews];
[self setCorner:UIRectCornerBottomRight | UIRectCornerTopLeft
bounds:self.activityNameView.bounds
cornerSize:CGSizeMake(4, 4)
targetView:self.activityNameView
layer:self.activityNameViewShapLayer];
}
activityNameView's bounds don't change
The default implementation uses any constraints you have set to determine the size and position of any subviews.
layoutSubviews only computes the size and position of its subviews. It does not lay out the whole view subtree.
After UITableViewCell.layoutSubviews, only the content view's frame is right.
The easier way to fix your layout is to call contentView.layoutIfNeeded inside layoutSubviews.

Dynamically resize UICollectionView's supplementary view (containing multiline UILabel's)

Inside a UICollectionView's supplementary view (header), I have a multiline label that I want to truncate to 3 lines.
When the user taps anywhere on the header (supplementary) view, I want to switch the UILabel to 0 lines so all text displays, and grow the collectionView's supplementary view's height accordingly (preferably animated). Here's what happens after you tap the header:
Here's my code so far:
// MyHeaderReusableView.m
// my gesture recognizer's action
- (IBAction)onHeaderTap:(UITapGestureRecognizer *)sender
{
self.listIntro.numberOfLines = 0;
// force -layoutSubviews to run again
[self setNeedsLayout];
[self layoutIfNeeded];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.listTitle.preferredMaxLayoutWidth = self.listTitle.frame.size.width;
self.listIntro.preferredMaxLayoutWidth = self.listIntro.frame.size.width;
[self layoutIfNeeded];
CGFloat height = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
self.frame = ({
CGRect headerFrame = self.frame;
headerFrame.size.height = height;
headerFrame;
});
NSLog(#"height: %#", #(height));
}
When I log height at the end of layoutSubviews, its value is 149 while the label is truncated and numberOfLines is set to 3. After tapping the headerView, setting numberOfLines to 0, and forcing a layout pass, height then gets recorded as 163.5. Great!
The only problem is that the entire headerView doesn't grow, and the cells don't get pushed down.
How can I dynamically change the height of my collectionView's supplementary view (preferably animated)?
I'm aware of UICollectionViewFlowLayout's headerReferenceSize and collectionView:layout:referenceSizeForHeaderInSection: but not quite sure how I'd use them in this situation.
I got something working, but I'll admit, it feels kludgy. I feel like this could be accomplished with the standard CollectionView (and associated elements) API + hooking into standard layout/display invalidation, but I just couldn't get it working.
The only thing that would resize my headerView was setting my collection view's flow layout's headerReferenceSize. Unfortunately, I can't access my collection view or it's flow layout from my instance of UICollectionReusableView, so I had to create a delegate method to pass the correct height back.
Here's what I have now:
// in MyHeaderReusableView.m
//
// my UITapGestureRecognizer's action
- (IBAction)onHeaderTap:(UITapGestureRecognizer *)sender
{
self.listIntro.numberOfLines = 0;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.listTitle.preferredMaxLayoutWidth = self.listTitle.frame.size.width;
self.listIntro.preferredMaxLayoutWidth = self.listIntro.frame.size.width;
CGFloat height = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
self.frame = ({
CGRect headerFrame = self.frame;
headerFrame.size.height = height;
headerFrame;
});
if (self.resizeDelegate) {
[self.resizeDelegate wanderlistDetailHeaderDidResize:self.frame.size];
}
}
// in my viewController subclass which owns the UICollectionView:
- (void)wanderlistDetailHeaderDidResize:(CGSize)newSize
{
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
// this is the key line
flowLayout.headerReferenceSize = newSize;
// this doesn't look beautiful but it's the best i can do for now. I would love for just the bottom of the frame to animate down, but instead, all the contents in the header (the top labels) have a crossfade effect applied.
[UIView animateWithDuration:0.3 animations:^{
[self.collectionView layoutIfNeeded];
}];
}
Like I said, not the solution I was looking for, but a working solution nonetheless.
I ran into the same issue than you, so I was just wondering: did you ever get a solution without the crossfade effect that you mention in the code sample?. My approach was pretty much the same, so I get the same problem. One additional comment though: I managed to implement the solution without the need for delegation: What I did was from "MyHeaderReusableView.m" You can reference the UICollectionView (and therefore, the UICollectionViewLayout) by:
//from MyHeaderReusableView.m
if ([self.superview isKindOfClass:UICollectionView.class]) {
//get collectionView reference
UICollectionView * collectionView = (UICollectionView*)self.superview;
//layout
UICollectionViewFlowLayout * layout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;
//... perform the header size change
}

UICollectionViewCell subview have wrong position (autolayout)

I'm using UICollectionView and subclassed cell. Cell is loaded from a xib where I layout a few subviews. Inside the cell I have a BOOL that change one of the subviews contraint. On iOS this works. But on iOS 7 after the constraint is changed, the position of this subview is wrong. If I logged the info in console, the constraint is set to 0 but the frame show the 188.
I know there is a bug about contentView resizing, and I handle that well with setting autoresizing mask. But what about this problem.
This are the constraints for this subview:
So inside the code when the BOOL is set to YES, the topConstraint is set from 0 to (-self.thisSubview.bounds.size.height).
If I log bounds height it tells me that its 130 so the origin.x should be now -130 but its -188 actually.
This is the code where I change the contstaint:
- (void)setHasInfo:(BOOL)hasInfo {
if (hasInfo) {
self.infoViewTopCon.constant = 0;
[self layoutIfNeeded];
}
else {
self.infoViewTopCon.constant = - self.infoView.bounds.size.height + 4.0;
[self layoutIfNeeded];
}
_hasInfo = hasInfo;
}
Update the NSLayoutConstraints to make it work.

How to change the size of tableview programmatically?

I try to change the size (the width) of a table viewcontroller (the panel with text over syria):
I have tried :
-(void)viewWillAppear:(BOOL)animated
{
self.tableView.frame = CGRectMake(0, 310, self.view.frame.size.width,200);
}
but it doesn't work. Do you know how can I reduce the width of the TableView ?
Thanks for your help !
You will have to add the table view as a subview to your view controllers view with the new size in viewWillAppear
Check the view frame width to make sure it is actually less than 320 before setting it. Most likely you are setting the table view width back to 320 which is the entire width.
Also if you are just setting the width of the table I would recommend doing the following.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.width = 250;
self.tableView.frame = tableViewFrame;
}

Resources