0

I create a view which inherits UIView and add it to a superView. But when I check the view's frame, I found it has been changed. But my code doesn't do it. I don't know why. Let's see the Code.

- (void)addBottomView
{
    NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed:@"WYOrderDetailBottomView" owner:nil options:nil];
    if (viewArray.count > 0) {
        self.bottomContentView = viewArray.firstObject;
    }

    self.bottomContentView.delegate = self;
    self.bottomContentView.frame = CGRectMake(0.0f, 0.0f, SCREENWIDTH, 0.0f);
    MLOG(@"%@", NSStringFromCGRect(self.bottomContentView.frame));
    self.bottomViewHeightConstraint.constant = 0.0f;
    [self updateViewConstraints];
    [self.view layoutIfNeeded];
    MLOG(@"%@", NSStringFromCGRect(self.bottomContentView.frame));
    [self.bottomBackView addSubview:self.bottomContentView];
    MLOG(@"%@", NSStringFromCGRect(self.bottomContentView.frame));
}

this method is invoked in viewDidLoad like this, the log of view's frame is (0,0,375,0) in iphone6.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"预约单详情";

    self.isNeedRefreshUI = YES;
    [self addBottomView];
    [self configTableView];
    [self addNotificationObserver];
}

But in another method, I check the frame of the view(self.bottomContentView).

- (void)getOrderDetailInfo
{
    MLOG(@"%@", NSStringFromCGRect(self.bottomContentView.frame));
}

the frame changed to (0, 0, 430, 0), I don't write any code to change it's frame. How it works?

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
huixing
  • 321
  • 2
  • 11
  • when you add a subview in code, some autolayout constraints get added automatically. might be something to do with that. Try adding your own constraints. – ShahiM Dec 22 '15 at 06:21
  • Did you run it in various resolutions or iphone 6 alone? – S. Karthik Dec 22 '15 at 06:24
  • Can you please post your layoutSubviews method? I think that is creating some issue – Arun Dec 22 '15 at 06:24
  • call this method [self addBottomView]; in viewWillAppear or viewDidAppear – Smile Dec 22 '15 at 06:33
  • @huixing if you are using Storyboard or Xib that autolayout is setting your frame automatically. call this method [self addBottomView]; in viewWillAppear or viewDidAppear to set the frame manually – Smile Dec 22 '15 at 06:36
  • @ismail, when I call this method in viewWillAppear, the width also changed to 430. But when I call method in viewDidAppear, it is normal(375) – huixing Dec 22 '15 at 06:45
  • @huixing self.bottomContentView.frame = CGRectMake(0.0f, 0.0f, SCREENWIDTH, 0.0f); this line is there which is changing the width – Smile Dec 22 '15 at 06:54
  • @ismail but the screenwidth is 375 not 430 – huixing Dec 22 '15 at 06:57
  • @huixing self.bottomContentView.frame = CGRectMake(0.0f, 0.0f, SCREENWIDTH, 0.0f) comment this line and check or set autoresizing to false – Smile Dec 22 '15 at 06:58
  • @ismail, I can't understand. the ScreenWidth is 375, but why I call the method(addBottomView) in viewDidAppear is normal(375), but in ViewDidLoad or viewWillAppear it is 430(not 375) – huixing Dec 22 '15 at 07:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98648/discussion-between-ismail-and-huixing). – Smile Dec 22 '15 at 07:03
  • http://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle Apple Docs https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/ check this – Smile Dec 22 '15 at 07:07
  • @ismail sorry,my network to visit stack is too slow. so the time between my comments is very long. I will see this two link. thank you for your enthusiastic – huixing Dec 22 '15 at 07:17

3 Answers3

2

if you are using Storyboard or Xib that autolayout or constaint is setting your frame automatically. call this method [self addBottomView]; in viewWillAppear or viewDidAppear to set the frame manually check this

Stackoverflow and Apple Documentation

Community
  • 1
  • 1
Smile
  • 667
  • 1
  • 5
  • 21
1
[self.view layoutIfNeeded] 

In constraints this method call the any updated frames are waiting in the view for show the new view. Please check it out in this code.

S. Karthik
  • 618
  • 5
  • 16
1

May be you have width constraint in xib file (or autoresizing mask constraints in the runtime). You can see this constraint by printing it in console, for example, you can use po command:

po self.view.constraints

Also if you use autolayout you shouldn't change frames of views directly, you should only change constraint constant value.

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40
  • when I use this po command, it create an error like this.error: property 'layoutConstraints' not found on object of type 'UIView *' – huixing Dec 22 '15 at 06:51
  • Sorry, my mistake. Correct name of the property is constraints. P.S. you should call this not for only self.view, also for bottomContentView and for bottomBackView – Andrew Romanov Dec 22 '15 at 07:16
  • But it also create an error.I don't know why. the result like this:(lldb) po self.view.constraints error: property 'constraints' not found on object of type 'UIView *' error: 1 errors parsing expression – huixing Dec 22 '15 at 07:25
  • This property available since iOS 6 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/constraints . Your error is very strange. I've tried my code now and all fine (I've copied and pasted code from here). Result: (lldb) po self.view.constraints <__NSArrayI 0x7ca73eb0>( , – Andrew Romanov Dec 22 '15 at 08:07
  • very interesting... What version of Xcode are you using? – Andrew Romanov Dec 22 '15 at 08:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98656/discussion-between-huixing-and-andrew-romanov). – huixing Dec 22 '15 at 08:24
  • Xcode 7.2, I po the constraint, I find this no constraint about bottomContentView in self.view, but in bottomContentView I use constraints to layout bottomContentView's subView. – huixing Dec 22 '15 at 08:42