UISlider changing width depending on device

左心房为你撑大大i 提交于 2019-12-13 02:26:19

问题


I'm a beginner in iOS programming and I'm looking for an answer to my question.

I have a UISlider inside a UITableViewCell and I'm trying to get this disposition: Label-Slider-DetailLabel(dynamic).

This is my code :

cell = [tableView dequeueReusableCellWithIdentifier:@"SelectedMeetingCell"];
[cell.textLabel setText:@"Duration"];
[cell.detailTextLabel setText:[Utilities StringFromDuration:Agenda.InitialDuration.doubleValue]];

sliderCell = (SliderCell*)[[UIViewController alloc] initWithNibName:@"SliderCell" bundle:nil].view;
[sliderCell setDelegate:self];
[sliderCell.slider setValue:Agenda.InitialDuration.doubleValue];

[cell.contentView addSubview:sliderCell];

sliderCell.slider.frame = CGRectMake(100, 0, 500, 44);

The last line is the way I used to place my slider the way I wanted to be. It works great for iPad but not at all for the iPhone version. Is there any way to resize the width of my slider automatically depending on the device ?

(SliderCell is a UITableViewCell with a UISlider @property)

Thank you all in advance!


回答1:


Instead of hard coding the values, you could get the container views width and set the sliders width as a percentage of it.

CGRect superViewFrame = slider.superView.frame;
CGRect sliderFrame = slider.frame;
sliderFrame.size.width = <your choice  for eg, superViewFrame.size.width * 0.3f>
sliderFrame.origin.x = <your choice for eg, superViewFrame.size.width * .2f>
slider.frame = sliderFrame;



回答2:


you can get the size of a device by calling self.view

sliderCell.slider.frame = CGRectMake(100, 0, self.view.frame.size.width, self.view.frame.size. height);

after you may want to subtract or divide depending on your view's size



来源:https://stackoverflow.com/questions/12884363/uislider-changing-width-depending-on-device

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