When to use layoutSubview in iOS

I am writing an iOS app for iPad that requires a custom layout.

The layout from the portrait and the landscape is completely different, so it cannot be solved using the UIAutoResizingMask.

I am trying to use the layoutSubview method, but found that the subview layout is called a lot (from UIScrollView). How can I reduce the call to layoutSubview to optimize the code, or should I call it myself when the device is ever rotated.

Thank.

+5
source share
4 answers

The fact that layoutSubviewsis called by the child UIScrollViewis very unsuccessful, but there is a (ugly) workaround:

@interface MyClass : UIView {
    BOOL reallyNeedsLayout_;
}
@end

@implementation MyClass

- (void)setNeedsLayout
{
    [super setNeedLayout];
    reallyNeedsLayout_ = YES;
}

- (void)setFrame:(CGRect)rect
{
    [super setFrame:rect];
    reallyNeedsLayout_ = YES;
}

- (void)layoutSubviews
{
    if (!reallyNeedsLayout_) return;
    reallyNeedsLayout_ = NO;

    // Do layouting.
}
@end

, , , .

+3

,

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

, UIDeviceOrientationDidChangeNotification .

init~:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangedOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];

- (void) didChangedOrientation:(NSNotification *)sender{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(orientation)){}}
+4

layoutSubviews:

+1

, deviceDidRotateSelector. updatePortrait updateLandscape , .

0
source

All Articles