我正在使用自動佈局。以編程方式將UIScrollView添加到UIView不能在viewDidLoad中工作,但在viewDidAppear中工作?
我像下面的代碼一樣以編程方式向uview查看scrollview。我試圖運行initShopView在視圖加載但它只是不工作,並沒有添加滾動視圖來查看。我已經看到了視圖層次結構的捕獲。
class variables:
@property(strong,nonatomic) UIScrollView *shopScrollView;
@property(strong,nonatomic) UIView *headView;
@property(strong,nonatomic) UIButton *favoriteButton;
- (void)viewDidLoad {
[super viewDidLoad];
[self initShopView];// not work
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self initShopView];// will work
}
-(void)initShopView{
self.shopScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.superview.frame.size.height - slideTitleHeight)];
self.shopScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 800);
self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
self.favoriteButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 60, 10, 55, 55)];
[self.favoriteButton setTitle:@"Favorite" forState:UIControlStateNormal];
[self.favoriteButton setImage:[UIImage imageNamed:@"favoriteGreen.png"] forState:UIControlStateNormal];
[self.headView addSubview:self.favoriteButton];
[self.shopScrollView addSubview:self.headView];
[self.view addSubview:self.shopScrollView];
}
@Phillip Mills給出瞭解決方案。我的滾動視圖幀
self.shopScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.superview.frame.size.height - slideTitleHeight)];
解決的辦法是:
self.shopScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - slideTitleHeight)];
你在哪裏使用autolayout?對於以編程方式創建的視圖,您必須以編程方式添加自動佈局。 –