简体   繁体   English

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

[英]How To Add button in a Slide-out Sidebar Menu in ios programmatically

I am pretty new in objective c so hopefully this all make sense.. I have declared UIView in .h file 我在目标c上还很新,所以希望这一切都说得通..我在.h文件中声明了UIView

@property (strong, nonatomic) UIView *menuViewone; 

In .m file i declared UIView in viewdidload 在.m文件中,我在viewdidload中声明了UIView

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

and set the view frame in Button method 并在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) {

        }];
    }}

Now i want to declare the buttons in this UIView ...How i can do that programmatically? 现在我想在此UIView中声明按钮...如何以编程方式做到这一点?

Just add buttons to the subview of that menuview of yours. 只需在您的菜单视图的子视图中添加按钮即可。

It would look something like this in the viewDidLoad of yours right after you declared your menuview. 声明menuview之后,在您的viewDidLoad中看起来像这样。

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

For more specifics you can check this out: How do I create a basic UIButton programmatically? 有关更多详细信息,您可以查看以下内容: 如何以编程方式创建基本的UIButton?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM