gpt4 book ai didi

ios - 在父 View 上更新 NSLayoutConstraint

转载 作者:行者123 更新时间:2023-11-29 00:36:58 24 4
gpt4 key购买 nike

如何更改/更新父 View 上的约束。

我使用 CodeBlock 的尝试:

//
// CollectionViewController.m
// FloatingButton
//

#import <QuartzCore/QuartzCore.h>
#import "CollectionViewController.h"
#import "MyCollectionViewCell.h"

#import "FloatingView.h"

#define CircleWidthAndHight 80

@interface CollectionViewController ()

@end

@implementation CollectionViewController{
FloatingView *floatingView;
CGRect rectOrigin;
NSLayoutConstraint *hightConstraint;

}

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
[super viewDidLoad];

self.collectionView.scrollEnabled = YES;

// CGFloat hightOfFloatingView = self.collectionView.frame.size.height - (self.collectionView.frame.size.height - 100);
// CGFloat yPositionOfFloatingView = self.collectionView.frame.size.height - (hightOfFloatingView + 10);


floatingView = [[FloatingView alloc] initWithCircleSize:CircleWidthAndHight];
floatingView.backgroundColor = [UIColor greenColor];
__block id myself = self;
floatingView.compare = ^{
[myself showFloatingButtons];

};

[self.view addSubview:floatingView];
[self.view bringSubviewToFront:floatingView];


// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;

// Register cell classes
// [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

// Do any additional setup after loading the view.
}

- (void)createTabBar{

}

- (void)viewDidLayoutSubviews{
[self setConstraints];
}

- (void) showFloatingButtons{

hightConstraint.constant = 200;

[self.view layoutIfNeeded];

// [self.view removeConstraint:hightConstraint];
//
// hightConstraint = [NSLayoutConstraint
// constraintWithItem:floatingView
// attribute:NSLayoutAttributeHeight
// relatedBy:NSLayoutRelationEqual
// toItem:nil
// attribute:NSLayoutAttributeNotAnAttribute
// multiplier:1.0
// constant:CircleWidthAndHight +200];
//
//
//
// [self.view addConstraint:hightConstraint];


}

- (void)setConstraints{

floatingView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:floatingView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:CircleWidthAndHight]];

hightConstraint = [NSLayoutConstraint
constraintWithItem:floatingView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:CircleWidthAndHight +20];



[self.view addConstraint:hightConstraint];
// floatingView.parentHightConstraint = hightConstraint;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:floatingView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];

NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:floatingView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];

[self.view addConstraint:yConstraint];

// floatingView.parentYConstraint = yConstraint;

[floatingView setConstraints];
}



- (void)viewDidAppear:(BOOL)animated{
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = (MyCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];


cell.label.textColor = [UIColor blackColor];
cell.label.text = @"Hello there!";

cell.layer.borderWidth=1.0f;
cell.layer.borderColor=[UIColor blueColor].CGColor;

// Configure the cell

return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.collectionView.frame.size.width, 100);
}

#pragma mark ScrollView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// CGRect collBounds = self.collectionView.bounds;
// CGRect floatFrame = floatingView.frame;
// floatingView.frame = CGRectMake(rectOrigin.origin.x,
// rectOrigin.origin.y + collBounds.origin.y,
// floatFrame.size.width,
// floatFrame.size.height);
//
// [self.view bringSubviewToFront:floatingView];
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
*/

@结束

这不起作用,并给出了一个:

Unable to simultaneously satisfy constraints.

我试过“layoutIfNeeded”和“setNeedsDisplay”和“needsUpdateConstraints”。而且没有效果?我在约束(hightConstrint)工作后立即更改它,但不是从 CodeBlock。

也试过这个:

hightConstraint.active = false;
hightConstraint.constant = 400;
hightConstraint.active = true;

终端记录如下:

2016-11-02 13:10:36.900317 FloatingButton[32486:1582882] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60800008ac30 FloatingView:0x7f98adc0a670.height == 100 (active)>",
"<NSLayoutConstraint:0x600000087580 FloatingView:0x7f98adc0a670.height == 400 (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000087580 FloatingView:0x7f98adc0a670.height == 400 (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

最佳答案

当您更改约束的值(在您的情况下是常量)时,您必须在该约束适用的 super View 上调用 layoutIfNeeded

我假设 floatingView 是约束所附加的,在这种情况下,调用 [self.view layoutIfNeeded]

虽然根据我在您的回答中看到的代码(也许我遗漏了一些东西)您还有 3 个问题:

  1. 永远不要将 floatingView 添加到 View Controller 的 View 中。
  2. 永远不要将约束添加到 floatingView
  3. 您有其他限制条件,这些限制条件与您对该限制条件所做的不一致。我建议检查所有约束的逻辑,如果需要,请将它们绘制在白板等物理表面上(总是对我有帮助)。

关于ios - 在父 View 上更新 NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com