Add UISLider with Autolayout programmatically

萝らか妹 提交于 2019-12-07 11:50:53

问题


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]];

回答1:


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



来源:https://stackoverflow.com/questions/21810358/add-uislider-with-autolayout-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!