我寫了下面的代碼爲我的應用程序:UIButton設置框架不工作?
self.PlayButton = [[UIButton alloc] init];
//Setup Play Button
[self.PlayButton setTranslatesAutoresizingMaskIntoConstraints:NO];
self.PlayButton.frame = CGRectMake(self.view.frame.size.width * 0.38, self.view.frame.size.height * 0.666, self.view.frame.size.width * 0.48, self.view.frame.size.height * 0.29);
[self.PlayButton setBackgroundImage:[UIImage imageNamed:@"PlayButton.png"] forState:UIControlStateNormal];
[self.PlayButton.layer setMagnificationFilter:kCAFilterNearest];
[self.PlayButton addTarget:self action:@selector(PlayButtonMethod) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside]
[self.view addSubview:self.PlayButton];
但是,當這個代碼運行它,而不是出現在它只是出現在視圖的左上角一個相當具體位置的圖像。幾乎像它已被設置
self.PlayButton.frame = CGRectMake(0, 0, self.view.frame.size.width * 0.48, self.view.frame.size.height * 0.29);
因爲寬度和高度被放在正確的,但無論出於何種原因由CGRectMake設置按鈕的位置沒有考慮到這是奇怪的。我已經做了一些關於在代碼中創建UIButtons的研究,而且從我看到的情況來看,不僅這些發生在其他人身上,代碼編寫完全負有責任。
任何幫助,將不勝感激。 謝謝
我的第一個猜測是它是一個自動佈局問題。至於如何解決它我不確定,但你可能想嘗試做initWithFrame:看看是否有所作爲。 – 2015-02-10 01:25:35
@SkylerLauren剛剛給了一個嘗試,沒有什麼區別:/這個問題真的讓我失望......它發生在我所有的3個按鈕上。 – OllysEdits 2015-02-10 13:59:12