Clone or Copy UIViewController or UIView

前端 未结 3 1084
野的像风
野的像风 2020-12-17 14:43

UII need copy, self controller or self.view, i have tried:

UIView* viewOfSelf =[self.view copy];
UIViewController* controller = [self copy];

UIView* viewOfS         


        
3条回答
  •  我在风中等你
    2020-12-17 15:19

    Unfortunately I am not yet allowed to comment, but rishi's solution has one problem: it does not copy NSLayoutConstraints. So if any of your views are using autolayout, those are not going to get copied. I am writing a mini-lib to solve that and will post it to github when done. Unfortunately, I haven't found a ready-to-use solution anywhere.

    //Edit

    Sorry, what I wrote previously was not completely correct. I did some more research and it turns out that on default, the NSLayoutConstraints in the archived view are NOT saved. But, you can actually specify if you want to archive a constraint by setting its shouldBeArchived property to YES. Then, even after this fake copy, you retain the right constraints in the copied view.

提交回复
热议问题