代码之家  ›  专栏  ›  技术社区  ›  live-love

是否有uiview调整大小事件?

  •  92
  • live-love  · 技术社区  · 15 年前

    我有一个视图,其中包含ImageView的行和列。

    如果调整了此视图的大小,我需要重新排列图像视图的位置。

    此视图是另一个已调整大小的视图的子视图。

    是否有方法检测何时调整此视图的大小?

    7 回复  |  直到 8 年前
        1
  •  91
  •   Community Mohan Dere    9 年前

    layoutSubviews

    bounds When does an associated object get released?

    [yourView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
    

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if (object == yourView && [keyPath isEqualToString:@"bounds"]) {
            // do your stuff, or better schedule to run later using performSelector:withObject:afterDuration:
        }
    }
    
        2
  •  80
  •   Rudolf Adamkovič    8 年前

    UIView

    override var bounds: CGRect {
        didSet {
            // ...
        }
    }
    

    var boundsObservation: NSKeyValueObservation?
    
    func beginObservingBounds() {
        boundsObservation = observe(\.bounds) { capturedSelf, _ in
            // ...
        }
    }
    
        3
  •  26
  •   gwang    15 年前

        4
  •  7
  •   Adlai Holler    12 年前

    -(void)setBounds:(CGRect)newBounds {
        BOOL const isResize = !CGSizeEqualToSize(newBounds.size, self.bounds.size);
        if (isResize) [self prepareToResizeTo:newBounds.size]; // probably saves 
        [super setBounds:newBounds];
        if (isResize) [self recoverFromResizing];
    }
    

    setFrame: frame center bounds transform

        5
  •  7
  •   Warren Stringer    8 年前

    private var observer: NSKeyValueObservation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        observer = view.layer.observe(\.bounds) { object, _ in
            print(object.bounds)
        }
        // ...
    }
    override func viewWillDisappear(_ animated: Bool) {
        observer?.invalidate()
        //...
    }
    
        6
  •  6
  •   rekle    15 年前

    - (void) setFrame:(CGRect)frame
    {
      // Call the parent class to move the view
      [super setFrame:frame];
    
      // Do your custom code here.
    }
    
        7
  •  -3
  •   Dan Rosenstark    9 年前

    viewDidLayoutSubviews

    override func viewDidLayoutSubviews() {
        // update subviews
    }
    
    推荐文章