2016-08-25 36 views
28

我轉我的項目到iOS的10和8的XCode新的測試版本在我的應用程序的所有三個領域,我使用display:clipsToBounds導致的UIImage不要在iOS10和XCode的8

imageView.layer.cornerRadius = imageView.frame.size.width/2 
imageView.clipsToBounds = true 

相關的圖像根本不顯示。我試圖清理項目以及構建文件夾,重新啓動設備,嘗試各種模擬器,重新添加imageView,以編程方式設置關聯的UIImage而不是從資產中選擇一個。

刪除clipsToBounds線顯示矩形圖像,不管masksToBoundstrue還是false。如何在XCode8/iOS10中製作圓形圖像?

編輯:項目是Swift 2.x,但尚未更新爲Swift 3.0語法。

+0

所以它刪除了clipsToBounds設置語句,並在cornerRadius停止影響再次成爲直角的ImageView和你的形象? – ddb

+0

@ddb是的,刪除clipsToBounds語句後,圖像是矩形的並且是可見的。 – cloudcal

+1

這樣的回答:http://stackoverflow.com/questions/39380128/ios-10-gm-with-xcode-8-gm-causes-views-to-disappear-due-to-roundedcorners建議呼籲self.layoutIfNeeded –

回答

0

這可能是一個佈局問題。將clipToBounds設置爲false將顯示圖像,即使其大小爲零。你能打印你的圖像的frame

如果您在viewDidLoad中設置cornerRadiusclipToBounds屬性,請嘗試在viewDidLayoutSubviews()中執行此操作。

您也可以嘗試致電self.view.layoutIfNeeded()

+0

我最初嘗試在viewDidLoad和viewDidAppear中設置圓角半徑和clipsToBounds,但沒有結果。我試圖與viewDidLayoutSubviews無濟於事。 當我進入調試視圖層次結構時,我可以看到被繪製的UIImageView中的UIImage框架 - 只是沒有顯示圖像。 它也值得一提(我忘了在我的原始發佈),該項目仍是Swift 2.x,並沒有更新到Swift 3.0語法。 – cloudcal

+0

打印幀產量:(137.5,24.0,100.0,100.0) – cloudcal

-1

你應該叫imageView.frame.size.width

[self.view layoutSubviews]; 
imageView.layer.cornerRadius = imageView.frame.size.width/2 
imageView.clipsToBounds = true 
+0

您不應該手動調用'layoutSubviews'。致電'layoutIfNeeded'而不是 – Thorax

6

這個問題也發生在我身上之前調用layoutSubviews()主視圖。

我將這些代碼imageView.layer.cornerRadius = imageView.frame.size.width/2- (void)awakeFromNib改爲- (void)drawRect:(CGRect)rect並且此問題消失。

我的imageView通過自動佈局調整大小。我認爲,當從iOS 10的筆尖醒來時,高度和寬度並不是決定的。

+0

爲我工作!謝謝 –

+0

omg ..這樣一個簡單的靈魂.. +1 ..謝謝:) –

0

使用obj-c,將語法從viewDidLoad移動到viewDidLayoutSubviews爲我工作。

0

我只是實現圓角的這樣

@implementation RoundImageView 

- (instancetype)initWithCoder:(NSCoder *)coder 
{ 
    self = [super initWithCoder:coder]; 
    if (self) { 
     self.layer.masksToBounds = YES; 
     self.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width)/2; 
     [self addObserver:self 
       forKeyPath:@"bounds" 
        options:NSKeyValueObservingOptionNew 
        context:(__bridge void * _Nullable)(self)]; 
    } 
    return self; 
} 

-(void)dealloc 
{ 
    [self removeObserver:self 
       forKeyPath:@"bounds" 
       context:(__bridge void * _Nullable)(self)]; 
} 

-(void)observeValueForKeyPath:(NSString *)keyPath 
        ofObject:(id)object 
         change:(NSDictionary<NSString *,id> *)change 
         context:(void *)context 
{ 
    if(context == (__bridge void * _Nullable)(self) && object == self && [keyPath isEqualToString:@"bounds"]) 
    { 
     self.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width)/2; 
    } 
} 

@end 

,所以我總是正確圓角。 我沒有升級到iOS10和XCode8的問題

0

我認爲問題的發生是因爲我們設置圓角和剪輯子視圖爲@Pranoy C說。

我解決了使用layoutIfNeeded爲我的tableview單元格顯示用戶的個人資料圖片的問題。我想確保圖像是圓角

代碼如下: - (void)awakeFromNib {super awakeFromNib]; [self layoutIfNeeded];

[self.inputIndicator_imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
self.profile_pic_edit_imageView.layer.cornerRadius = self.profile_pic_edit_imageView.frame.size.width/2; 
self.profile_pic_edit_imageView.layer.borderWidth = 1.0; 
self.profile_pic_edit_imageView.layer.borderColor = GRAY_COLOR_SUPER_LIGHT.CGColor; 
self.profile_pic_edit_imageView.clipsToBounds = YES;} 
1

我有同樣的問題。沒有在手機上顯示,但完美的故事板和UI調試器。當我把這個項目打開爲「用Xcode 7打開」而不是8時,它也在工作,但它不是一個令人滿意的解決方案。所以我挖掘並發現了這個問題。麻煩的是一類看起來像這樣:

@IBDesignable public class MyButton: UIButton { 

    @IBInspectable var styleName: String = "" { 
     didSet { 
      switch styleName { 
      case "RoundedLight": 
       tintColor = UIColor.lightPinkColor() 
       layer.borderColor = UIColor.whiteColor().CGColor 
       layer.borderWidth = 1 
       layer.masksToBounds = true 
      default: 
       break 
      } 
     } 
    } 

    override public func layoutSubviews() { 
     super.layoutSubviews() 

     switch styleName { 
      case "RoundedLight": 
       layer.cornerRadius = frame.height/2 
      default: 
       break 
     } 
    } 
} 

我改成了這一點,它現在的作品:

@IBDesignable public class MyButton: UIButton { 

    @IBInspectable var styleName: String = "" { 
     didSet { 
      layoutIfNeeded() 
     } 
    } 

    override public func layoutSubviews() { 
     super.layoutSubviews() 

     switch styleName { 
     case "RoundedLight": 
      tintColor = UIColor.lightPinkColor() 
      layer.borderColor = UIColor.whiteColor().CGColor 
      layer.borderWidth = 1 
      layer.cornerRadius = frame.height/2 
      layer.masksToBounds = true 
     default: 
      break 
     } 
    } 
} 

注意,沒有以上的工作對我來說,我的意思是:

  • layoutSubviews()
  • 調用layoutIfNeededawakeFromNib調用layoutIfNeeded我的按鈕(或包含按鈕我的小區)
  • 在我的小區的layoutSubview
  • 在我的細胞
  • 在我的視圖控制器調用view.layoutIfNeeded()
2

這聲音的awakeFromNib調用contentView.layoutIfNeeded()調用layoutIfNeeded就像它可能是由於iOS 10和Xcode 8的一個新bug導致視圖初始化爲1000x1000,並且嘗試將角半徑設置爲幀的一半時,它將其設置爲500。這裏在這個問題上進一步記錄:Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews。我之所以這樣想,是因爲在做任何需要在界面構建器上構建的東西的大小之前調用佈局子視圖的1000x1000問題的修復。

1

我的ImageView完全是圓形的。下面是代碼:

//MARK:- Misc functions 
func setProfileImage() { 
    imgProfile.layer.masksToBounds = false 
    imgProfile.layer.cornerRadius = imgProfile.frame.size.height/2 
    imgProfile.clipsToBounds = true 
    imgProfile.contentMode = UIViewContentMode.ScaleToFill 
} 

這工作正常的iOS 9然而,in iOS 10 Xcode 8 the ImageView disappears. 調試後發現,ClipsToBound是罪魁禍首。

所以放在viewDidLayoutSubviews()的代碼解決了這個問題。

override func viewDidLayoutSubviews() { 
    setProfileImage() 
} 

您還可以使用self.view.layoutIfNeeded()