Skip to content

Commit dc20470

Browse files
committed
replace SDAutoLayout with UIStackView
1 parent fb59111 commit dc20470

File tree

3 files changed

+137
-117
lines changed

3 files changed

+137
-117
lines changed

example/iosApp/Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ target 'iosApp' do
55
platform :ios, '14.0'
66
pod 'kmp_webrtc', :path => '../../kmp-webrtc/kmp_webrtc.podspec'
77
pod 'WebRTC', :path => '../../libs/apple/WebRTC.podspec'
8-
pod 'SDAutoLayout', '~> 2.2.1'
98
pod 'kmp_xlog', '~> 1.3.1'
109
end

example/iosApp/iosApp/CallViewController.m

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#import "ARDSettingsModel.h"
3131
#import "ARDToast.h"
3232

33-
@import SDAutoLayout;
3433
@import WebRTC;
3534
@import kmp_webrtc;
3635

@@ -40,25 +39,24 @@ @interface CallViewController () <Kmp_webrtcPeerConnectionClientCallback, CFAudi
4039
@implementation CallViewController {
4140
ARDSettingsModel* _settingsModel;
4241
bool _isLandscape;
43-
42+
4443
Kmp_webrtcObjCPeerConnectionClientFactory* _pcClientFactory;
4544
id<Kmp_webrtcPeerConnectionClient> _pcClient;
4645
NSTimer* _statsTimer;
4746
CFAudioMixer* _mixer;
48-
49-
UIView* _rootLayout;
47+
5048
CFEAGLVideoView* _remoteRenderer;
5149
CFEAGLVideoView* _localRenderer;
52-
50+
5351
UIButton* _leaveButton;
5452
UIButton* _recordButton;
5553
UIButton* _mixerButton;
5654
UIButton* _switchCameraButton;
57-
55+
5856
bool _recording;
5957
bool _mixingMusic;
6058
bool _videoEnabled;
61-
59+
6260
bool _sendLastFrame;
6361
bool _left;
6462
}
@@ -77,18 +75,47 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
7775
: UIInterfaceOrientationMaskPortrait;
7876
}
7977

80-
- (void)loadView {
81-
CGSize fc = [UIScreen mainScreen].bounds.size;
82-
CGFloat fcWidth = _isLandscape ? fc.height : fc.width;
83-
CGFloat fcHeight = _isLandscape ? fc.width : fc.height;
78+
- (void)viewDidLoad {
79+
[super viewDidLoad];
8480

85-
self.view =
86-
[[UIView alloc] initWithFrame:CGRectMake(0, 0, fcWidth, fcHeight)];
81+
_localRenderer =
82+
[[CFEAGLVideoView alloc] initWithFrame:CGRectZero
83+
andUid:@"test_local"
84+
andScaleType:CF_SCALE_TYPE_CENTER_CROP];
85+
_localRenderer.mirror = YES;
86+
_localRenderer.autoresizingMask =
87+
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
88+
89+
_remoteRenderer =
90+
[[CFEAGLVideoView alloc] initWithFrame:CGRectZero
91+
andUid:@"test_remote"
92+
andScaleType:CF_SCALE_TYPE_CENTER_CROP];
93+
_remoteRenderer.mirror = NO;
94+
_remoteRenderer.autoresizingMask =
95+
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
8796

88-
_rootLayout = [[UIView alloc] init];
89-
[self.view addSubview:_rootLayout];
90-
_rootLayout.sd_layout.widthRatioToView(self.view, 1)
91-
.heightRatioToView(self.view, 1);
97+
UIView* localWrapper = [[UIView alloc] init];
98+
[localWrapper addSubview:_localRenderer];
99+
UIView* remoteWrapper = [[UIView alloc] init];
100+
[remoteWrapper addSubview:_remoteRenderer];
101+
102+
UIStackView *mainStackView = [[UIStackView alloc] init];
103+
mainStackView.axis = UILayoutConstraintAxisVertical;
104+
mainStackView.distribution = UIStackViewDistributionFillEqually;
105+
mainStackView.alignment = UIStackViewAlignmentFill;
106+
mainStackView.spacing = 0;
107+
mainStackView.translatesAutoresizingMaskIntoConstraints = NO;
108+
109+
[mainStackView addArrangedSubview:localWrapper];
110+
[mainStackView addArrangedSubview:remoteWrapper];
111+
[self.view addSubview:mainStackView];
112+
113+
[NSLayoutConstraint activateConstraints:@[
114+
[mainStackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
115+
[mainStackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
116+
[mainStackView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
117+
[mainStackView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
118+
]];
92119

93120
_recordButton = [UIButton buttonWithType:UIButtonTypeSystem];
94121
[_recordButton setTitle:_recording ? @"stop record" : @"start record"
@@ -97,11 +124,6 @@ - (void)loadView {
97124
[_recordButton addTarget:self
98125
action:@selector(onRecord:)
99126
forControlEvents:UIControlEventTouchUpInside];
100-
[self.view addSubview:_recordButton];
101-
_recordButton.sd_layout.widthIs(120)
102-
.heightIs(20)
103-
.bottomSpaceToView(self.view, 30)
104-
.leftSpaceToView(self.view, 10);
105127

106128
_mixerButton = [UIButton buttonWithType:UIButtonTypeSystem];
107129
[_mixerButton setTitle:_mixingMusic ? @"stop mixer" : @"start mixer"
@@ -110,89 +132,72 @@ - (void)loadView {
110132
[_mixerButton addTarget:self
111133
action:@selector(onMixer:)
112134
forControlEvents:UIControlEventTouchUpInside];
113-
[self.view addSubview:_mixerButton];
114-
_mixerButton.sd_layout.widthIs(120)
115-
.heightIs(20)
116-
.bottomEqualToView(_recordButton)
117-
.leftSpaceToView(_recordButton, 10);
118135

119136
_leaveButton = [UIButton buttonWithType:UIButtonTypeSystem];
120137
[_leaveButton setTitle:@"Leave" forState:UIControlStateNormal];
121138
_leaveButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
122139
[_leaveButton addTarget:self
123140
action:@selector(onLeaveCall:)
124141
forControlEvents:UIControlEventTouchUpInside];
125-
[self.view addSubview:_leaveButton];
126-
_leaveButton.sd_layout.widthIs(80)
127-
.heightIs(20)
128-
.bottomEqualToView(_recordButton)
129-
.leftSpaceToView(_mixerButton, 10);
142+
143+
UIStackView *ops1 = [[UIStackView alloc] init];
144+
ops1.axis = UILayoutConstraintAxisHorizontal;
145+
ops1.distribution = UIStackViewDistributionFillEqually;
146+
ops1.alignment = UIStackViewAlignmentFill;
147+
ops1.spacing = 0;
148+
ops1.translatesAutoresizingMaskIntoConstraints = NO;
149+
150+
[ops1 addArrangedSubview:_recordButton];
151+
[ops1 addArrangedSubview:_mixerButton];
152+
[ops1 addArrangedSubview:_leaveButton];
153+
[self.view addSubview:ops1];
154+
155+
[NSLayoutConstraint activateConstraints:@[
156+
[ops1.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
157+
[ops1.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
158+
[ops1.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
159+
[ops1.heightAnchor constraintGreaterThanOrEqualToConstant:30]
160+
]];
130161

131162
_switchCameraButton = [UIButton buttonWithType:UIButtonTypeSystem];
132163
[_switchCameraButton setTitle:@"FRONT" forState:UIControlStateNormal];
133164
_switchCameraButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
134165
[_switchCameraButton addTarget:self
135166
action:@selector(onSwitchCamera:)
136167
forControlEvents:UIControlEventTouchUpInside];
137-
[self.view addSubview:_switchCameraButton];
138-
_switchCameraButton.sd_layout.widthIs(80)
139-
.heightIs(20)
140-
.leftEqualToView(_recordButton)
141-
.bottomSpaceToView(_recordButton, 20);
142168

143169
UIButton* videoButton = [UIButton buttonWithType:UIButtonTypeSystem];
144170
[videoButton setTitle:@"Video" forState:UIControlStateNormal];
145171
videoButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
146172
[videoButton addTarget:self
147173
action:@selector(onToggleVideo:)
148174
forControlEvents:UIControlEventTouchUpInside];
149-
[self.view addSubview:videoButton];
150-
videoButton.sd_layout.widthIs(80)
151-
.heightIs(20)
152-
.bottomEqualToView(_switchCameraButton)
153-
.leftSpaceToView(_switchCameraButton, 10);
154175

155176
UIButton* sendLastFrameButton = [UIButton buttonWithType:UIButtonTypeSystem];
156177
[sendLastFrameButton setTitle:@"SLF" forState:UIControlStateNormal];
157178
sendLastFrameButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
158179
[sendLastFrameButton addTarget:self
159180
action:@selector(onToggleSendLastFrame:)
160181
forControlEvents:UIControlEventTouchUpInside];
161-
[self.view addSubview:sendLastFrameButton];
162-
sendLastFrameButton.sd_layout.widthIs(80)
163-
.heightIs(20)
164-
.bottomEqualToView(_switchCameraButton)
165-
.leftSpaceToView(videoButton, 10);
166-
167-
_localRenderer =
168-
[[CFEAGLVideoView alloc] initWithFrame:CGRectZero
169-
andUid:@"test_local"
170-
andScaleType:CF_SCALE_TYPE_CENTER_CROP];
171-
_localRenderer.mirror = YES;
172-
_localRenderer.autoresizingMask =
173-
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
174-
175-
_remoteRenderer =
176-
[[CFEAGLVideoView alloc] initWithFrame:CGRectZero
177-
andUid:@"test_remote"
178-
andScaleType:CF_SCALE_TYPE_CENTER_CROP];
179-
_remoteRenderer.mirror = NO;
180-
_remoteRenderer.autoresizingMask =
181-
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
182182

183-
UIView* wrapper = [[UIView alloc] init];
184-
[_rootLayout insertSubview:wrapper atIndex:0];
185-
wrapper.sd_layout.widthRatioToView(_rootLayout, 1)
186-
.heightRatioToView(_rootLayout, 0.5)
187-
.topEqualToView(_rootLayout);
188-
[wrapper addSubview:_localRenderer];
189-
190-
wrapper = [[UIView alloc] init];
191-
[_rootLayout insertSubview:wrapper atIndex:0];
192-
wrapper.sd_layout.widthRatioToView(_rootLayout, 1)
193-
.heightRatioToView(_rootLayout, 0.5)
194-
.bottomEqualToView(_rootLayout);
195-
[wrapper addSubview:_remoteRenderer];
183+
UIStackView *ops2 = [[UIStackView alloc] init];
184+
ops2.axis = UILayoutConstraintAxisHorizontal;
185+
ops2.distribution = UIStackViewDistributionFillEqually;
186+
ops2.alignment = UIStackViewAlignmentFill;
187+
ops2.spacing = 0;
188+
ops2.translatesAutoresizingMaskIntoConstraints = NO;
189+
190+
[ops2 addArrangedSubview:_switchCameraButton];
191+
[ops2 addArrangedSubview:videoButton];
192+
[ops2 addArrangedSubview:sendLastFrameButton];
193+
[self.view addSubview:ops2];
194+
195+
[NSLayoutConstraint activateConstraints:@[
196+
[ops2.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
197+
[ops2.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
198+
[ops2.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-60],
199+
[ops2.heightAnchor constraintGreaterThanOrEqualToConstant:30]
200+
]];
196201

197202
[self startLoopback];
198203
}

example/iosApp/iosApp/HallViewController.m

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#import "CallViewController.h"
1212
#import "ARDToast.h"
1313

14-
@import SDAutoLayout;
1514
@import WebRTC;
1615

1716
static NSString* const barButtonImageString = @"ic_settings_black_24dp.png";
@@ -22,57 +21,74 @@ @implementation HallViewController {
2221

2322
- (void)viewDidLoad {
2423
[super viewDidLoad];
25-
24+
2625
self.title = @"kmp-webrtc";
2726
self.view.backgroundColor = [UIColor whiteColor];
2827
[self addSettingsBarButton];
29-
30-
UIButton* create = [UIButton buttonWithType:UIButtonTypeSystem];
31-
[create setTitle:@"loopback" forState:UIControlStateNormal];
32-
create.titleLabel.font = [UIFont systemFontOfSize:20];
33-
[self.view addSubview:create];
34-
create.sd_layout.widthIs(180)
35-
.heightIs(20)
36-
.topSpaceToView(self.view, 30)
37-
.centerXEqualToView(self.view);
38-
39-
[create addTarget:self
40-
action:@selector(onLoopback:)
41-
forControlEvents:UIControlEventTouchUpInside];
42-
43-
UITableViewCell* audioOnlyCell =
44-
[[UITableViewCell alloc] initWithFrame:CGRectZero];
45-
audioOnlyCell.selectionStyle = UITableViewCellSelectionStyleNone;
46-
_audioOnlySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
47-
audioOnlyCell.accessoryView = _audioOnlySwitch;
48-
audioOnlyCell.textLabel.text = @"Audio only";
49-
[self.view addSubview:audioOnlyCell];
50-
audioOnlyCell.sd_layout.widthIs(180)
51-
.heightRatioToView(create, 1)
52-
.topSpaceToView(create, 20)
53-
.centerXEqualToView(self.view);
28+
29+
UIButton* loopback = [UIButton buttonWithType:UIButtonTypeSystem];
30+
[loopback setTitle:@"loopback" forState:UIControlStateNormal];
31+
[loopback addTarget:self
32+
action:@selector(onLoopback:)
33+
forControlEvents:UIControlEventTouchUpInside];
34+
loopback.titleLabel.font = [UIFont systemFontOfSize:20];
35+
36+
UIStackView *switchStackView = [[UIStackView alloc] init];
37+
switchStackView.axis = UILayoutConstraintAxisHorizontal;
38+
switchStackView.distribution = UIStackViewDistributionFillProportionally;
39+
switchStackView.spacing = 10;
40+
UILabel *switchLabel = [[UILabel alloc] init];
41+
switchLabel.text = @"Audio only";
42+
switchLabel.font = [UIFont systemFontOfSize:20];
43+
[switchStackView addArrangedSubview:switchLabel];
44+
_audioOnlySwitch = [[UISwitch alloc] init];
45+
[switchStackView addArrangedSubview:_audioOnlySwitch];
5446

5547
UIButton* shareLog = [UIButton buttonWithType:UIButtonTypeSystem];
5648
[shareLog setTitle:@"share log" forState:UIControlStateNormal];
57-
shareLog.titleLabel.font = [UIFont systemFontOfSize:20];
58-
[self.view addSubview:shareLog];
59-
shareLog.sd_layout.widthIs(180)
60-
.heightRatioToView(create, 1)
61-
.topSpaceToView(audioOnlyCell, 20)
62-
.centerXEqualToView(self.view);
63-
6449
[shareLog addTarget:self
6550
action:@selector(onShareLog:)
6651
forControlEvents:UIControlEventTouchUpInside];
52+
shareLog.titleLabel.font = [UIFont systemFontOfSize:20];
6753

6854
UILabel* version = [[UILabel alloc] initWithFrame:CGRectZero];
6955
version.text = [NSString stringWithFormat:@"kmp-webrtc %@", [CFPeerConnectionClient versionName]];
70-
version.textAlignment = NSTextAlignmentCenter;
71-
[self.view addSubview:version];
72-
version.sd_layout.widthIs(250)
73-
.heightRatioToView(create, 1)
74-
.topSpaceToView(shareLog, 10)
75-
.centerXEqualToView(self.view);
56+
version.font = [UIFont systemFontOfSize:20];
57+
58+
UIStackView *stackView = [[UIStackView alloc] init];
59+
stackView.axis = UILayoutConstraintAxisVertical;
60+
stackView.alignment = UIStackViewAlignmentLeading;
61+
stackView.distribution = UIStackViewDistributionFillEqually;
62+
stackView.spacing = 10;
63+
64+
[stackView addArrangedSubview:loopback];
65+
[stackView addArrangedSubview:switchStackView];
66+
[stackView addArrangedSubview:shareLog];
67+
[stackView addArrangedSubview:version];
68+
[self.view addSubview:stackView];
69+
70+
// 使用 Auto Layout 约束 StackView 的位置和大小
71+
stackView.translatesAutoresizingMaskIntoConstraints = NO;
72+
// 水平居中
73+
NSLayoutConstraint *centerXConstraint =
74+
[NSLayoutConstraint constraintWithItem:stackView
75+
attribute:NSLayoutAttributeCenterX
76+
relatedBy:NSLayoutRelationEqual
77+
toItem:self.view
78+
attribute:NSLayoutAttributeCenterX
79+
multiplier:1.0
80+
constant:0];
81+
// 距离顶部 50
82+
NSLayoutConstraint *topConstraint =
83+
[NSLayoutConstraint constraintWithItem:stackView
84+
attribute:NSLayoutAttributeTop
85+
relatedBy:NSLayoutRelationEqual
86+
toItem:self.view.safeAreaLayoutGuide
87+
attribute:NSLayoutAttributeTop
88+
multiplier:1.0
89+
constant:50];
90+
// 激活约束
91+
[NSLayoutConstraint activateConstraints:@[centerXConstraint, topConstraint]];
7692
}
7793

7894
- (void)viewDidAppear:(BOOL)animated {

0 commit comments

Comments
 (0)