如何以编程方式在ios中的滑出边栏菜单中添加按钮

时间:2014-04-02 07:20:45

标签: ios iphone objective-c uiview

我在目标c中相当新,所以希望这一切都有意义..我已经在.h文件中声明了UIView

@property (strong, nonatomic) UIView *menuViewone; 

在.m文件中,我在viewdidload中声明了UIView

menuViewone =[[UIView alloc ]initWithFrame:CGRectMake(-400, 0, 200, 568) ];
[menuViewone setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:menuViewone];

并在Button方法

中设置视图框
 - (IBAction)collectionmenutwo:(id)sender {
    if (menuViewone.frame.origin.x >=0 ) {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            menuViewone.frame=CGRectMake(-400, 100, 300, 568);
        } completion:^(BOOL finished) {

        }];
    }

    else
    {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            menuViewone.frame=CGRectMake(200, 100, 300, 568);
        } completion:^(BOOL finished) {

        }];
    }}

现在我想声明这个UIView中的按钮...我怎么能以编程方式做到这一点?

1 个答案:

答案 0 :(得分:1)

只需将按钮添加到您的菜单视图的子视图中即可。

在宣布菜单视图后,它会在你的viewDidLoad中看起来像这样。

UIButton *btn = [[UIButton] alloc] init];
btn.frame = CGRectMake(0,0,100,100);
//set other button properties here.
[menuViewone addSubview:btn];

有关详细信息,请查看以下内容: How do I create a basic UIButton programmatically?

相关问题