Tag: 自动布局

在容器视图中加载ViewController

我有一个VC全屏幕的容器视图。 如果我从故事板手动添加一个孩子到containerView做一个embeddedsegue看起来不错: 但我通过代码embeddedVC: class BannerContainerVC: UIViewController { @IBOutlet weak var container: UIView! override func viewDidLoad() { super.viewDidLoad() let vc = storyboard?.instantiateViewControllerWithIdentifier("test") as UIViewController self.container.addSubview(vc.view) } } 我得到超级奇怪的结果:

实现以编程方式生成的视图的自动布局

我有一个应用程序的视图是以编程方式生成的。 例: -(void)loadView { [super loadView]; // SET TOP LEFT BTN FOR NEXT VIEW UIBarButtonItem *topLeftBtn = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]; self.navigationItem.backBarButtonItem = topLeftBtn; [topLeftBtn release]; // programmatically set up the view for cart tableView CGRect iouTableViewFrame = CGRectMake(0, 0, 320, 348); iouTableView = [[UITableView alloc]initWithFrame:iouTableViewFrame style:UITableViewStylePlain]; [[self iouTableView] setDelegate:self]; [[self iouTableView] setDataSource:self]; […]

iOS Autolayout垂直等于填充父视图的空间

我有一个与12 UITextField视图控制器。 它非常适合3.5英寸的显示屏。 我需要将其设置为iPhone 5(4英寸显示器),以便所有UITextField通过在两者之间添加额外空间来覆盖整个UIView 。 我想通过自动布局来做到这一点,但它不能正常工作。 这是我的代码: – (void) viewWillLayoutSubviews { int h = txt1.bounds.size.height * 12; float unusedHorizontalSpace = self.view.bounds.size.height – h ; NSNumber* spaceBetweenEachButton= [NSNumber numberWithFloat: unusedHorizontalSpace / 13 ] ; NSMutableArray *constraintsForButtons = [[NSMutableArray alloc] init]; [constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|-50-[txt1(==30)]-(space)-[txt2(==txt1)]-(space)-[txt3(==txt1)]-(space)-[txt4(==txt1)]-(space)-[txt5(==txt1)]-(space)-[txt6(==txt1)]-(space)-[txt7(==txt1)]-(space)-[txt8(==txt1)]-(space)-[txt9(==txt1)]-(space)-[txt10(==txt1)]-(space)-[txt11(==txt1)]-(space)-[txt12]-(space)-|" options: NSLayoutFormatAlignAllCenterX metrics: @{@"space":spaceBetweenEachButton} views: NSDictionaryOfVariableBindings(txt1,txt10,txt11,txt12,txt2,txt3,txt4,txt5,txt6, txt7,txt8,txt9)]]; [self.view addConstraints:constraintsForButtons]; } […]

iOS Autolayout:在resize的父视图中发布UILabels

我有一个只包含UILabel的视图。 这个标签包含多行文本。 父级具有可变宽度,可以用平移手势resize。 我的问题是,当我这样做resize的UILabel不重新计算其高度,使所有的内容仍然可见,它只是把它切断。 我已经设法修复它一点点的黑客,但它运行速度非常缓慢: – (void)layoutSubviews { CGSize labelSize = [self.labelDescription sizeThatFits:CGSizeMake(self.frame.size.width, FLT_MAX)]; if (self.constraintHeight) { [self removeConstraint:self.constraintHeight]; } self.constraintHeight = [NSLayoutConstraint constraintWithItem:self.labelDescription attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:labelSize.height]; [self addConstraint:self.constraintHeight]; [super layoutSubviews]; } 这不应该与自动布局自动发生? 编辑 我的观点的结构是: UIScrollView —> UIView —> UILabel 这里是UIScrollView的约束: <NSLayoutConstraint:0x120c4860 H:|-(>=32)-[DescriptionView:0x85c81c0] (Names: '|':UIScrollView:0x85db650 )>, <NSLayoutConstraint:0x120c48a0 H:|-(32@900)-[DescriptionView:0x85c81c0] priority:900 (Names: '|':UIScrollView:0x85db650 )>, […]

如何dynamic更改自动布局iOS中的字体大小?

我想适合我的文字在一个UILabel ,但对于不同的iPhone UILabel的大小正在改变,因为我使用自动布局,但我不能修复字体大小,所以我的文字被删除。 有什么办法可以设置任何约束,以便文本dynamic适应UILabel ? 由于不同的屏幕分辨率,在这里看到的文字被削减

UIButtonresize以适应titleLabel

我有一个UIButton,我添加到故事板中的视图控制器视图。 我添加了居中约束来定位它,并引导空间约束来限制它的宽度。 在代码中我添加: self.button.titleLabel.numberOfLines = 0; self.button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; [self.button setTitle:@"A real real real real real real real real long long name." forState:UIControlStateNormal]; self.button.backgroundColor = [UIColor redColor]; self.button.titleLabel.backgroundColor = [UIColor blueColor]; 结果如下所示: 我想要button来调整其内容。 我怎样才能做到这一点? 我试过了 [self.button sizeToFit]; 我已经尝试将内容拥抱和压缩阻力自动布局约束优先级设置为需要。 我也试着明确地设置contentEdgeInsets和titleEdgeInsets到UIEdgeInsetsZero并调用invalidateIntrinsicContentsize。 我也注意到,如果我在标题string中放置换行符,button似乎resize以适应其内容。 我在iPhone 6模拟器上运行Xcode 6和iOS 8。

更改UIImageView大小以使图像与AutoLayout匹配

我想改变我的UIImageView的高度/宽度,以匹配我从相册中select的图像,我在这里find了一个片段,这样做。 -(CGRect)frameForImage:(UIImage *)imgs inImageViewAspectFit:(UIImageView *)imagev { float imageRatio = imgs.size.width / imgs.size.height; float viewRatio = imagev.frame.size.width / imagev.frame.size.height; if(imageRatio < viewRatio) { float scale = imagev.frame.size.height / imgs.size.height; float width = scale * imgs.size.width; float topLeftX = (imagev.frame.size.width – width) * 0.5; NSLog(@"image after aspect fit: width=%f",width); imagev.frame = CGRectMake(topLeftX, 0, width, imagev.frame.size.height); return CGRectMake(topLeftX, […]

在8.0之前的iOS版本中布局相对于版面边距的属性

什么会导致下面的警告(和iOS 7的后续alignment问题)? 属性不可用:在8.0版本以前的iOS版本中布局相对于版面边距的属性

在UITableViewCell上遇到AutoLayout问题

我在xcode 5项目上遇到了自动布局问题。 我正在使用导航控制器内部的普通视图控制器。 上半部分有一个MKMapView ,下半部分有一个UITableView 。 我正在使用storyboards ,并configuration了原型UITableViewCell ,但我通过代码添加约束。 我已经仔细检查了原型中的每个控件,并且没有看到任何configuration的约束。 我为UITableViewCell添加约束时发生问题。 我在单元格中有以下代码: -(void)updateConstraints { [super updateConstraints]; //first remove old constraints [self removeConstraints:self.constraints]; [self.nameLabel removeConstraints:self.nameLabel.constraints]; [self.addressLabel removeConstraints:self.nameLabel.constraints]; [self.rentableSquareFeetLabel removeConstraints:self.rentableSquareFeetLabel.constraints]; [self.lastSaleAmountLabel removeConstraints:self.lastSaleAmountLabel.constraints]; [self.lastSaleDateLabel removeConstraints:self.lastSaleAmountLabel.constraints]; [self.thumbnailImageView removeConstraints:self.thumbnailImageView.constraints]; //then set up constraints NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_thumbnailImageView, _nameLabel, _rentableSquareFeetLabel, _lastSaleAmountLabel, _addressLabel, _lastSaleDateLabel); [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_thumbnailImageView(60)]-[_nameLabel(<=200)]-(>=8)-[_rentableSquareFeetLabel]-(>=8)-[_lastSaleAmountLabel]|" options:0 metrics:nil views:viewsDictionary]]; [self addConstraints:[NSLayoutConstraint […]

使用Autolay的animation/移动视图

我想从一个位置移动到另一个位置,我可以使用它来实现它 self.view.center = CGPointMake(100, 200); 但是,如果项目使用Autolayout,那么运行后视图将回到原始位置: [self.view.superview setNeedsLayout]; 那么如何将视图移到新的位置?