在ios中创建类似UiActionSheet的自定义uiView

时间:2016-05-23 12:19:48

标签: ios objective-c iphone uiview uiactionsheet

enter image description here   我想显示一个ActionSheet来显示地图上的创建帐户我可以通过使用ShowFromRect方法在ipad中实现这个,但是,ShowFromRect的行为没有为Iphone定义,或者它有默认的bahaviour,它从视图的底部弹出但是我想在用户点击地图的任何地方显示ActionSheet。我该如何实现呢?或者就像我必须创建一个像UIActionSheet这样的自定义UIView,箭头指向用户在地图上选择的位置。如果是这样,我如何为此目的创建自定义UIView?          我附上了一个演示图像,以显示我希望它看起来像。

1 个答案:

答案 0 :(得分:0)

可能是这个解决方案可以帮助你...我在某个地方读到这个..你可以使用动作表来做到这一点。 首先在界面声明uiactionsheet的属性。在下面的例子中,我声明了一个名为countryActionSheet的属性。

之后在IBaction中遵循代码;

 -(IBAction)popupView{//  declare another actionSheet //
UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"Select Country"       delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];

countryActionsheet=actionSheet;//equate both the actionSheets//

actionSheet.actionSheetStyle= UIActionSheetStyleBlackTranslucent;
countryPicker.dataSource=self;
countryPicker.delegate=self; 

// Add your view with frame with respect to actionSheet//

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
imageView.image=[UIImage imageNamed:@"black background.png"];

UIButton *cancelBtn=[[UIButton alloc]initWithFrame:CGRectMake(200,20, 80, 30)];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancelBtnPressed) forControlEvents:UIControlEventTouchUpInside];// create a method named cancelBtnPressed
[cancelBtn setBackgroundImage:[UIImage imageNamed:@"Untitled 4.png"] forState:UIControlStateNormal];

UIButton *okBtn =[[UIButton alloc]initWithFrame:CGRectMake(40, 20, 80, 30)];
[okBtn setTitle:@"Done" forState:UIControlStateNormal];
[okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[okBtn addTarget:self action:@selector(okBtnPressed) forControlEvents:UIControlEventTouchUpInside];// create method named okBtnPressed
[okBtn setBackgroundImage:[UIImage imageNamed:@"Untitled 4.png"] forState:UIControlStateNormal];


[countryActionsheet addSubview:imageView];
[countryActionsheet addSubview:cancelBtn];
[countryActionsheet addSubview:okBtn];

[countryActionsheet showInView:self.view];
[countryActionsheet setBounds:CGRectMake(0, 0, 320, 400)];// frame of actionSheet
}

-(void)cancelbtnPressed{
     // write action
}

-(void)okBtnPressed{
     // write action
}

希望这可以帮到你;