简体   繁体   中英

Can't change size and position for UIView loaded from xib

I have a UICollectionView and the dark grey area on the top. In this grey area I needed to put tabs with UIView . Because I'll need outlets, this UIView has its own TabsViewController . No matter what I do, the view I am trying to add is always stays in the top left corner with strange offset and size.

I tried to change it size and position or to put it on top of different views — it is always gives the same visual result.

Auto Layout is on. This view should have height of 72 and 100% width.

在此处输入图片说明

TabsViewController *tabs = [[TabsViewController alloc] init];
CGSize screen = // returns correct screen size
tabs.view.frame = CGRectMake(0, 0, screen.width, 72);
[self.collectionView addSubview:tabs.view];

I tried to call code above from multiple places, including viewDidLoad , viewWillAppear , viewDidLayoutSubviews and viewDidAppear .

In the auto layout you have to change the constrains instead of frame. Just connect your view constrains to code as and try changing your constrains to set your view programatically.

Connect constraint of your view in IB to code as IBOutlet properties

Example :

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hieght;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *horizontalCenter;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verticalCenter;

And change there values as:

self.width.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.hieght.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.topSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.horizontalCenter.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.leftSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.rightSpace.constant=CURRENT_VALUE_FOR_CONSTRAINT;
self.verticalCenter.constant=CURRENT_VALUE_FOR_CONSTRAINT;

此搜索图像2

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