我需要显示一个不是全屏的视图,并且在它下面有一个按钮和pickerView。 我尝试使用此代码:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20,20,200,200)];
container.backgroundColor=[UIColor whiteColor];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y+5, 170, 20); // position in the parent view and set the size of the button
myButton.titleLabel.textColor=[UIColor redColor];
myButton.titleLabel.text=@"click me";
//myButton.backgroundColor=[UIColor blueColor];
//[myButton backgroundImageForState:<#(UIControlState)#>[UIImage imageNamed:@"iPhone_mainbutton_green.png"];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add a buttonview
[container addSubview:myButton];
UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];
//piker.numberOfComponents=1;
piker.showsSelectionIndicator=YES;
//piker.delegate=self;
//piker.dataSource=self;
[container addSubview:piker];
[myButton release];
[piker release];
[self.view addSubview:container];
我得到了这个(选择器离开屏幕并且非常大,而不是100x100):
答案 0 :(得分:2)
你在容器和容器框架中添加pickeer视图是:-( 20,20,200,200)
将其(0,20,200,200)
。
答案 1 :(得分:2)
您正在将UIPickerView添加为“容器”的子视图
UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(container.frame.origin.x, container.frame.origin.y +30, 100, 100)];
这意味着如果你想让它在正确的位置,pickerview从容器而不是从UIView获取它的来源: -
UIPickerView *piker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 100)];
[container addSubview:picker];
只关注你的原点并记住每个子视图都从它的父视图中获取它。 还有一件事苹果不允许更改pickerview高度所以你不能设置它100x100你可以改变它的宽度。 UIPickerView仅支持3个高度值,这些是216.0,180.0,162.0尝试仅从这3个值设置高度。它会起作用。
如果您对此有任何疑问,请与我们联系。
答案 2 :(得分:0)
如果要调整框架,请在视图控制器的viewWillAppear:
方法中进行调整 - 您可以在那里进行调整。
确保您还实现widthForComponent:
并且所有组件的宽度合并小于帧宽 - 选择器插入。我使用以下内容:
- (void)viewDidLoad{
self.picker = [[[UIPickerView alloc] init] autorelease];
[self.view addSubview:self.picker];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.settingsPicker.frame = CGRectMake(0.0, 100.0, self.view.frame.size.width, 100.0);
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat guessedPickerInsetWidth = 24;
CGFloat pickerWidth = self.view.frame.size.width - guessedPickerInsetWidth;
if (component == SettingsPickerFirstComponent) {
return pickerWidth * 0.4; // make the first component 40%
}
return pickerWidth * 0.3; // only two others, make them 30% each
}