添加UISLider与自动布局编程 [英] Add UISLider with Autolayout programmatically

查看:155
本文介绍了添加UISLider与自动布局编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个UISlider添加到我的看法编程,包括限制,以便其宽度适应整个屏幕宽度。
这是我走到这一步:

  // 2添加UISlider
self.slider = [[UISlider的alloc]初始化];
[self.view addSubview:self.slider];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.slider
                                                      属性:NSLayoutAttributeLeft
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      属性:NSLayoutAttributeLeft
                                                     事半功倍:1
                                                       常数:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.slider
                                                      属性:NSLayoutAttributeRight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      属性:NSLayoutAttributeRight
                                                     事半功倍:1
                                                       常数:0]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.slider
                                                      属性:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      属性:NSLayoutAttributeBottom
                                                     事半功倍:1
                                                       常数:0]];


解决方案

缺少 self.slider.translatesAutoresizingMaskIntoConstraints = NO; 您code是我工作的罚款。见下面的图片

I'm trying to add a UISlider to my view programmatically including constraints so that its width adapts to the whole screen width. This is what I got so far:

//2 Add UISlider
self.slider = [[UISlider alloc] init];
[self.view addSubview:self.slider];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.slider
                                                      attribute:NSLayoutAttributeLeft 
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view 
                                                      attribute:NSLayoutAttributeLeft 
                                                     multiplier:1 
                                                       constant:0]];


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.slider
                                                      attribute:NSLayoutAttributeRight 
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view 
                                                      attribute:NSLayoutAttributeRight 
                                                     multiplier:1 
                                                       constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.slider
                                                      attribute:NSLayoutAttributeBottom 
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view 
                                                      attribute:NSLayoutAttributeBottom 
                                                     multiplier:1 
                                                       constant:0]];

解决方案

Missing self.slider.translatesAutoresizingMaskIntoConstraints = NO; Your code is working fine for me. See the below image

这篇关于添加UISLider与自动布局编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆