[英]backgroundImage on UIButton scales wrongly
當用戶按下按鈕然后設置寬度動畫時,我試圖給我的UIButton一個新的背景圖像。 問題是按鈕沒有縮放圖像(我只使用視網膜圖像)。
點擊之前:
點擊后:
以及使事情發生的代碼:
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
[self.profileButton removeConstraint:self.profileButtonConstraint];
// Animate
CGRect originalFrame = self.profileButton.frame;
originalFrame.size.width = 40;
[UIView animateWithDuration:0.2f
animations:^{
self.profileButton.frame = originalFrame;
}
completion:^(BOOL finished) {
}];
試試這個代碼......
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[self.profileButton setBackgroundImage:[UIImage imageNamed:@"Profile_Button_Default"] forState:UIControlStateNormal];
[self.profileButton setBackgroundImage:[UIImage imageNamed:@"Profile_Button_Default"] forState:UIControlStateSelected];
[self.profileButton setFrame:CGRectMake(self.profileButton.frame.origin.x, self.profileButton.frame.origin.y, self.profileButton.frame.size.width + 40, self.profileButton.frame.size.height)];
[UIView commitAnimations];
我希望這能幫到你......
創建邊緣插入的方式將使最左邊的3個和最右邊的3個(或像素)保持不變,並且它會拉伸按鈕上的頭部。 如果你想讓頭部的大小保持不變,你必須將它包含在左側,留下1個像素可拉伸,然后是右側。 假設你的圖片寬度為50,你會寫:
UIImage * buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 46, 3, 3)];
但是,這會使您的圖片位於按鈕的左側。 我推薦的解決方案是使用2張圖片:
代碼看起來像:
UIImage *backgroundImage = [[UIImage imageNamed:@"border.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)]; // in this case you can use 3, 3, 3, 3
[self.profileButton setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *titleImage = [UIImage imageNamed:@"head.png"];
[self.profileButton setImage:titleImage forState:UIControlStateNormal];
希望這可以幫助!
我已經修改了以下幾點的代碼:
現在檢查:
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
[self.profileButton setBackgroundImage:buttonProfileDefault
forState:UIControlStateSelected];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateHighlighted];
[self.profileButton removeConstraint:self.profileButtonConstraint];
// Animate
CGRect originalFrame = self.profileButton.frame;
originalFrame.size.width = 40;
[UIView animateWithDuration:0.2f
animations:^{
self.profileButton.frame = originalFrame;
}
completion:^(BOOL finished) {
}];
我認為問題是你改變背景然后調整大小。
讓它使用自動布局的關鍵是為widthConstraint常量值設置動畫,並在動畫中設置背景圖像。
例如,我的視圖名稱按鈕上有一個按鈕 ,我有一個名為buttonWidth的寬度約束的IBOutlet - 下面是我的buttonPress的代碼:
UIImage *imageButton = [[UIImage imageNamed:@"button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
[UIView animateWithDuration:0.01f
animations:^{
[self.button setBackgroundImage:imageButton forState:UIControlStateNormal];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:1.0f
animations:^{
self.buttonWidth.constant = 300;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
}];
正如您在上面的代碼中看到的那樣,使其適用於我的關鍵是在動畫中設置按鈕背景,出於某種原因,如果我在動畫之外更改背景圖像,則在動畫完成之前它將無法正確設置。
使用autoLayout時設置框架對我來說也不起作用,所以我為約束常量設置了動畫
試試這個,而不是將圖像指定為
UIImage *buttonProfileDefault = [[UIImage imageNamed:@"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
做這個
UIImage *buttonProfileDefault = [UIImage imageNamed:@"Profile_Button_Default"];
無需設置resizableImageWithCapInsets的約束。
我有同樣的問題,但它發生在我的應用程序加載時,我在檢查器中弄亂了button
的屬性,如果你向下滾動並autoresize subviews
檢查autoresize subviews
它可能會工作,它對我autoresize subviews
! 祝好運!
在使用筆尖時,我有類似的情況調整子視圖的大小。 不確定你是不是,但解決我的問題的是在我的各種觀點中取消選中“使用Autolayout”。
希望能解決您的問題。
布蘭登
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.