Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Apple authored and Apple committed Nov 6, 2018
1 parent 6c5e844 commit af380aa
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 21 deletions.
71 changes: 54 additions & 17 deletions JHFrameLayout/JHFrameLayoutView.m
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ - (JHLayoutBottomOfView)centerYOffsetBottomOfView{
@interface JHFrameLayoutView()
@property (nonatomic, assign) BOOL initFlag;
@property (nonatomic, assign) BOOL superviewObserveFlag;
@property (nonatomic, unsafe_unretained) UIView *superView;
@end

@implementation JHFrameLayoutView
Expand All @@ -687,13 +686,61 @@ - (instancetype)initWithFrame:(CGRect)frame
return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
[super willMoveToSuperview:newSuperview];

// newSuperview is not kind of JHFrameLayoutView
if (newSuperview && ![newSuperview isKindOfClass:[JHFrameLayoutView class]]) {

// self.superview may be observed before
if (_superviewObserveFlag && self.superview != nil && ![self.superview isKindOfClass:[JHFrameLayoutView class]]) {
@try {
[self.superview removeObserver:self forKeyPath:@"frame"];
} @catch (NSException *exception) {

} @finally {

}

@try {
[self.superview removeObserver:self forKeyPath:@"bounds"];
} @catch (NSException *exception) {

} @finally {

}
}

// observe newSuperview's frame and bounds
[newSuperview addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:NULL];
[newSuperview addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:NULL];
_superviewObserveFlag = YES;
}

// remove observer
if (newSuperview == nil && self.superview != nil && _superviewObserveFlag && ![self.superview isKindOfClass:[JHFrameLayoutView class]]) {
_superviewObserveFlag = NO;

@try {
[self.superview removeObserver:self forKeyPath:@"frame"];
} @catch (NSException *exception) {

} @finally {

}

@try {
[self.superview removeObserver:self forKeyPath:@"bounds"];
} @catch (NSException *exception) {

} @finally {

}
}
}

- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (_superviewObserveFlag) {
[_superView removeObserver:self forKeyPath:@"frame"];
[_superView removeObserver:self forKeyPath:@"bounds"];
[_superView removeObserver:self forKeyPath:@"center"];
}
}

- (void)layoutSubviews{
Expand Down Expand Up @@ -721,15 +768,6 @@ - (void)jh_initOnce

// Screen Rotate Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jh_delayLayoutSubviews) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

// Observe superview's frame
if (self.superview) {
_superviewObserveFlag = YES;
_superView = self.superview;
[self.superview addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
[self.superview addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:nil];
[self.superview addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
}
}
break;
}
Expand Down Expand Up @@ -762,8 +800,7 @@ - (void)jh_layoutView:(UIView *)view
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"frame"] ||
[keyPath isEqualToString:@"bounds"] ||
[keyPath isEqualToString:@"center"] ) {
[keyPath isEqualToString:@"bounds"]) {
[self layoutSubviews];
}
}
Expand Down
Binary file modified JHFrameLayoutDemo/.DS_Store
Binary file not shown.
16 changes: 14 additions & 2 deletions JHFrameLayoutDemo/JHFrameLayoutDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
087B7ECE20BE3AD100B14DD1 /* Demo9ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 087B7ECD20BE3AD100B14DD1 /* Demo9ViewController.m */; };
087B7ED120BE3C0500B14DD1 /* Demo10ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 087B7ED020BE3C0500B14DD1 /* Demo10ViewController.m */; };
087B7ED420BE3CD900B14DD1 /* Demo11ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 087B7ED320BE3CD900B14DD1 /* Demo11ViewController.m */; };
18F492AA219040BD0031E021 /* Demo13ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F492A9219040BD0031E021 /* Demo13ViewController.m */; };
18F492B6219169E50031E021 /* Demo14ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F492B5219169E50031E021 /* Demo14ViewController.m */; };
E32C20AD2125846F00680AAE /* Demo12ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E32C20AC2125846F00680AAE /* Demo12ViewController.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -96,6 +98,10 @@
087B7ED020BE3C0500B14DD1 /* Demo10ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Demo10ViewController.m; sourceTree = "<group>"; };
087B7ED220BE3CD900B14DD1 /* Demo11ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Demo11ViewController.h; sourceTree = "<group>"; };
087B7ED320BE3CD900B14DD1 /* Demo11ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Demo11ViewController.m; sourceTree = "<group>"; };
18F492A8219040BD0031E021 /* Demo13ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Demo13ViewController.h; sourceTree = "<group>"; };
18F492A9219040BD0031E021 /* Demo13ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Demo13ViewController.m; sourceTree = "<group>"; };
18F492B4219169E50031E021 /* Demo14ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Demo14ViewController.h; sourceTree = "<group>"; };
18F492B5219169E50031E021 /* Demo14ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Demo14ViewController.m; sourceTree = "<group>"; };
E32C20AB2125846F00680AAE /* Demo12ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Demo12ViewController.h; sourceTree = "<group>"; };
E32C20AC2125846F00680AAE /* Demo12ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Demo12ViewController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -238,6 +244,10 @@
087B7ED320BE3CD900B14DD1 /* Demo11ViewController.m */,
E32C20AB2125846F00680AAE /* Demo12ViewController.h */,
E32C20AC2125846F00680AAE /* Demo12ViewController.m */,
18F492A8219040BD0031E021 /* Demo13ViewController.h */,
18F492A9219040BD0031E021 /* Demo13ViewController.m */,
18F492B4219169E50031E021 /* Demo14ViewController.h */,
18F492B5219169E50031E021 /* Demo14ViewController.m */,
);
path = DemoVC;
sourceTree = "<group>";
Expand Down Expand Up @@ -390,6 +400,7 @@
087B7EBF20BD646000B14DD1 /* Demo4ViewController.m in Sources */,
087B7EC820BE37B500B14DD1 /* Demo7ViewController.m in Sources */,
087B7E8320BCE36900B14DD1 /* JHFrameLayoutView.m in Sources */,
18F492AA219040BD0031E021 /* Demo13ViewController.m in Sources */,
087B7EBC20BD523400B14DD1 /* Demo3ViewController.m in Sources */,
087B7ED120BE3C0500B14DD1 /* Demo10ViewController.m in Sources */,
087B7ECB20BE3A1300B14DD1 /* Demo8ViewController.m in Sources */,
Expand All @@ -401,6 +412,7 @@
087B7EB920BCF78100B14DD1 /* Demo2ViewController.m in Sources */,
087B7EC520BE348400B14DD1 /* Demo6ViewController.m in Sources */,
087B7E4D20BCE33D00B14DD1 /* AppDelegate.m in Sources */,
18F492B6219169E50031E021 /* Demo14ViewController.m in Sources */,
087B7ECE20BE3AD100B14DD1 /* Demo9ViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -575,7 +587,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.haocold.JHFrameLayoutDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "c30aaefd-682c-4565-aaf0-50fc4fc13174";
PROVISIONING_PROFILE_SPECIFIER = haocold_dev;
PROVISIONING_PROFILE_SPECIFIER = haocold.develop;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -593,7 +605,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.haocold.JHFrameLayoutDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "c30aaefd-682c-4565-aaf0-50fc4fc13174";
PROVISIONING_PROFILE_SPECIFIER = haocold_dev;
PROVISIONING_PROFILE_SPECIFIER = haocold.develop;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildLocationStyle</key>
<string>UseAppPreferences</string>
<key>CustomBuildLocationType</key>
<string>RelativeToDerivedData</string>
<key>DerivedDataLocationStyle</key>
<string>Default</string>
<key>EnabledFullIndexStoreVisibility</key>
<false/>
<key>IssueFilterStyle</key>
<string>ShowActiveSchemeOnly</string>
<key>LiveSourceIssuesEnabled</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>JHFrameLayoutDemo.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Demo12ViewController.h
// JHFrameLayoutDemo
//
// Created by 薛精豪 on 2018/8/16.
// Created by xuejinghao on 2018/8/16.
//

#import <UIKit/UIKit.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Demo12ViewController.m
// JHFrameLayoutDemo
//
// Created by 薛精豪 on 2018/8/16.
// Created by xuejinghao on 2018/8/16.
//

#import "Demo12ViewController.h"
Expand Down
16 changes: 16 additions & 0 deletions JHFrameLayoutDemo/JHFrameLayoutDemo/DemoVC/Demo13ViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Demo13ViewController.h
// JHFrameLayoutDemo
//
// Created by xuejinghao on 2018/11/5.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface Demo13ViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
131 changes: 131 additions & 0 deletions JHFrameLayoutDemo/JHFrameLayoutDemo/DemoVC/Demo13ViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// Demo13ViewController.m
// JHFrameLayoutDemo
//
// Created by xuejinghao on 2018/11/5.
//

#import "Demo13ViewController.h"
#import "JHFrameLayout.h"

@interface CustomView: JHFrameLayoutView
@end
@implementation CustomView
@end

@interface Demo13ViewController ()

@end

@implementation Demo13ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

/**
a custom view inherit JHFrameLayoutView
自定义的view 继承自 JHFrameLayoutView
*/

self.navigationItem.title = @"Demo13";

self.view.backgroundColor = [UIColor whiteColor];

CustomView *view = [[CustomView alloc] init];
view.frame = CGRectMake(10, CGRectGetMaxY(self.navigationController.navigationBar.frame) + 10, CGRectGetWidth(self.view.frame)-20, 300);
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];

UIView *layoutView = [[UIView alloc] init];
layoutView.backgroundColor = [UIColor lightGrayColor];
[view addSubview:layoutView];

UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
[layoutView addSubview:view1];

UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor greenColor];
[layoutView addSubview:view2];

UIView *view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor blueColor];
[layoutView addSubview:view3];

UIView *view4 = [[UIView alloc] init];
view4.backgroundColor = [UIColor purpleColor];
[layoutView addSubview:view4];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, CGRectGetHeight(self.view.frame) - 50, CGRectGetWidth(self.view.frame)-20 , 30);
button.backgroundColor = [UIColor lightGrayColor];
button.titleLabel.font = [UIFont systemFontOfSize:16];
[button setTitle:@"Change Red View Frame" forState:0];
[button setTitleColor:[UIColor blackColor] forState:0];
[button addTarget:self action:@selector(buttonEVent) forControlEvents:1<<6];
[self.view addSubview:button];

view.tag = 100;
layoutView.tag = 200;
view1.tag = 201;
view2.tag = 202;
view3.tag = 203;
view4.tag = 204;

layoutView.jhLayout
.topOffsetBottomOfView(15, self.navigationController.navigationBar, NO)
.leftIs(10)
.rightOffsetRightOfView(-20, self.view, YES)
.bottomOffsetBottomOfView(-10, view, YES);

view1.jhLayout
.topIs(10)
.leftIs(10)
.sizeIs(CGSizeMake(50, 50));

view2.jhLayout
.sizeIs(CGSizeMake(50, 50))
.rightOffsetRightOfView(-10, layoutView, NO)
.topIs(10);

view3.jhLayout
.sizeIs(CGSizeMake(50, 50))
.leftIs(10)
.bottomOffsetBottomOfView(-10, layoutView, NO);

view4.jhLayout
.sizeIs(CGSizeMake(50, 50))
.rightOffsetRightOfView(-10, layoutView, NO)
.bottomOffsetBottomOfView(-10, layoutView, NO);

}

- (void)buttonEVent
{
UIView *view = [self.view viewWithTag:100];
CGRect frame = view.frame;

if (frame.size.height == 300) {
frame.size.height = 400;
view.frame = frame;
}else{
frame.size.height = 300;
view.frame = frame;
}
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
16 changes: 16 additions & 0 deletions JHFrameLayoutDemo/JHFrameLayoutDemo/DemoVC/Demo14ViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Demo14ViewController.h
// JHFrameLayoutDemo
//
// Created by Apple on 2018/11/6.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface Demo14ViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit af380aa

Please sign in to comment.