简体   繁体   中英

load views from xib, and how to change views' frame?

I load a view from a nib, this view has several subviews,and are on the xib. Once I load this view from nib, I tried to change the subviews frame by using: view.subLabel.frame = CGRectMake(). However, this does not work and I do not know why. I disabled the autolayout property of the xib and it does not work as well. Anyone knows how to change views frame loaded from xibs? Thank you.

codes for init and change frame are as follows:

Init: GADNativeContentAdView *adView = [[[NSBundle mainBundle] loadNibNamed:@"loadingContent" owner:nil options:nil] firstObject];

Then change frame: adView.logoView.frame = CGRectMake(0, 0,50, 50);

but this does not work.

You should work with NSAutolayout and use NSAutoLayoutConstraints if you want to change the size or position of your subviews. For example declare a height constraint as a property, connect it in your xib, and change it this way:

myHeightConstraint.constant = newValue
UIView.animateWithDuration(duration, animations: {
        self.view.layoutIfNeeded()
})

user2053760,

When you instantiate a view using

GADNativeContentAdView *adView = [[[NSBundle mainBundle] loadNibNamed:@"loadingContent" owner:nil options:nil] firstObject];

you havent specified its frame so by default it is instantiated with frame (0,0,0,0). Then you went on to change the view which is inside this adView and set its frame to (0,0,50,50).

How do you expect the subView to be visible if the view which holds it itself is of size (0,0) ???

so first change the parent frame as

adView.frame = CGRectMake(0, 0,50, 50);

then you can set the frame for childView,

adView.logoView.frame = CGRectMake(0, 0,50, 50);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM