我嘗試顯示覆選標記(GitHub WVCheckMark),當我點擊按鈕。這裏是代碼,只有當我第二次點擊按鈕時纔有效。如何正確添加和顯示水龍頭UIView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mark = [[WVCheckMark alloc] init];//WithFrame:CGRectMake(50, 50, 100, 100)];
mark.backgroundColor = [UIColor clearColor];
//[mark setFrame:CGRectMake(100, 100, 100, 100)];
//[self.view addSubview:mark];
}
- (IBAction) btnShow{
[mark setFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:mark];
[mark setNeedsDisplay];
[mark updateConstraints];
[mark setNeedsLayout];
[mark start];
}
如果在viewDidLoad中使用addSubview,這就是我想要的(點擊並顯示)。
調用這個方法並沒有幫助。
[mark setNeedsDisplay];
[mark updateConstraints];
[mark setNeedsLayout];
是否可以添加子視圖並將其顯示在代碼的一個位置?
的完整代碼
#import "ViewController.h"
#import "TestCheckmark-Swift.h"
@interface ViewController()
@end
WVCheckMark *mark;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
mark = [[WVCheckMark alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
mark.backgroundColor = [UIColor clearColor];
[mark setFrame:CGRectMake(100, 100, 100, 100)];
//[self.view addSubview:mark];
}
- (IBAction) btnShow{
//[mark setFrame:CGRectMake(100, 100, 100, 100)];
//[self.view addSubview:mark];
[mark setNeedsDisplay];
[mark updateConstraints];
[mark setNeedsLayout];
[mark start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
調試'btnShow'。它被稱爲? (刪除三個setNeed ... updateCo ...) – shallowThought
根據viewCont生命週期調用方法'viewDidLoad','viewWillAppear'和'viewDidAppear',只需在'viewDidAppear'中添加按鈕,然後在其上應用任何代碼。 – vaibhav
@shallowThought是的,當我點擊它的第一次時btnShow調用 – olegi