gpt4 book ai didi

iphone - 如何以编程方式创建 UI 元素

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:54 26 4
gpt4 key购买 nike

每次用户点击按钮时,我都希望能够在单独的 UIViewController 中创建新的 UI 元素(UIProgressView 栏)。

我该怎么做?

最佳答案

要以编程方式创建 UIProgressView,只需使用 alloc 和 init、设置框架并添加 subview ,如下所示:

//.h

#import <UIKit/UIKit.h>

@interface ExampleViewController : UIViewController {

//create the ivar
UIProgressView *_progressView;

}
/*I like to back up my iVars with properties. If you aren't using ARC, use retain instead of strong*/
@property (nonatomic, strong) UIProgressView *progressView;

@end

//.m

@implementation
@synthesize progressView = _progressView;

-(void)viewDidLoad {
/* it isn't necessary to create the progressView here, after all you could call this code from any method you wanted to and it would still work*/
//allocate and initialize the progressView with the bar style
self.progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
//add the progressView to our main view.
[self.view addSubview: self.progressView];
//if you ever want to remove it, call [self.progressView removeFromSuperView];

}

关于iphone - 如何以编程方式创建 UI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9492697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com