Skip to content

Commit 1e885ed

Browse files
committed
Use LayoutAnchor
1 parent 959e589 commit 1e885ed

File tree

4 files changed

+37
-130
lines changed

4 files changed

+37
-130
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ tabPageViewController.isInfinity = true
104104

105105
## Requirements
106106

107-
iOS8+
107+
iOS9+
108108

109109
## Installation
110110

Sources/TabPageViewController.swift

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -137,43 +137,16 @@ extension TabPageViewController {
137137

138138
fileprivate func configuredTabView() -> TabView {
139139
let tabView = TabView(isInfinity: isInfinity, option: option)
140-
tabView.translatesAutoresizingMaskIntoConstraints = false
141-
142-
let height = NSLayoutConstraint(item: tabView,
143-
attribute: .height,
144-
relatedBy: .equal,
145-
toItem: nil,
146-
attribute: .height,
147-
multiplier: 1.0,
148-
constant: option.tabHeight)
149-
tabView.addConstraint(height)
150140
view.addSubview(tabView)
151141

152-
let top = NSLayoutConstraint(item: tabView,
153-
attribute: .top,
154-
relatedBy: .equal,
155-
toItem: topLayoutGuide,
156-
attribute: .bottom,
157-
multiplier:1.0,
158-
constant: 0.0)
159-
160-
let left = NSLayoutConstraint(item: tabView,
161-
attribute: .leading,
162-
relatedBy: .equal,
163-
toItem: view,
164-
attribute: .leading,
165-
multiplier: 1.0,
166-
constant: 0.0)
167-
168-
let right = NSLayoutConstraint(item: view,
169-
attribute: .trailing,
170-
relatedBy: .equal,
171-
toItem: tabView,
172-
attribute: .trailing,
173-
multiplier: 1.0,
174-
constant: 0.0)
175-
176-
view.addConstraints([top, left, right])
142+
tabView.translatesAutoresizingMaskIntoConstraints = false
143+
tabBarTopConstraint = tabView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor)
144+
NSLayoutConstraint.activate([
145+
tabView.heightAnchor.constraint(equalToConstant: option.tabHeight),
146+
tabBarTopConstraint!,
147+
tabView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
148+
tabView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
149+
])
177150

178151
tabView.pageTabItems = tabItems.map({ $0.title})
179152
tabView.updateCurrentIndex(beforeIndex, shouldScroll: true)
@@ -182,52 +155,23 @@ extension TabPageViewController {
182155
self?.displayControllerWithIndex(index, direction: direction, animated: true)
183156
}
184157

185-
tabBarTopConstraint = top
186-
187158
return tabView
188159
}
189160

190161
private func setupStatusView() {
191162
let statusView = UIView()
192163
statusView.backgroundColor = option.tabBackgroundColor
193-
statusView.translatesAutoresizingMaskIntoConstraints = false
194164
view.addSubview(statusView)
195165

196-
let top = NSLayoutConstraint(item: statusView,
197-
attribute: .top,
198-
relatedBy: .equal,
199-
toItem: view,
200-
attribute: .top,
201-
multiplier:1.0,
202-
constant: 0.0)
203-
204-
let left = NSLayoutConstraint(item: statusView,
205-
attribute: .leading,
206-
relatedBy: .equal,
207-
toItem: view,
208-
attribute: .leading,
209-
multiplier: 1.0,
210-
constant: 0.0)
211-
212-
let right = NSLayoutConstraint(item: view,
213-
attribute: .trailing,
214-
relatedBy: .equal,
215-
toItem: statusView,
216-
attribute: .trailing,
217-
multiplier: 1.0,
218-
constant: 0.0)
219-
220-
let height = NSLayoutConstraint(item: statusView,
221-
attribute: .height,
222-
relatedBy: .equal,
223-
toItem: nil,
224-
attribute: .height,
225-
multiplier: 1.0,
226-
constant: topLayoutGuide.length)
227-
228-
view.addConstraints([top, left, right, height])
229-
230-
statusViewHeightConstraint = height
166+
statusView.translatesAutoresizingMaskIntoConstraints = false
167+
statusViewHeightConstraint = statusView.heightAnchor.constraint(equalToConstant: topLayoutGuide.length)
168+
NSLayoutConstraint.activate([
169+
statusView.topAnchor.constraint(equalTo: view.topAnchor),
170+
statusView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
171+
statusView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
172+
statusViewHeightConstraint!
173+
])
174+
231175
self.statusView = statusView
232176
}
233177

Sources/TabView.swift

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,13 @@ internal class TabView: UIView {
4747
addSubview(contentView)
4848
contentView.backgroundColor = option.tabBackgroundColor.withAlphaComponent(option.tabBarAlpha)
4949

50-
let top = NSLayoutConstraint(item: contentView,
51-
attribute: .top,
52-
relatedBy: .equal,
53-
toItem: self,
54-
attribute: .top,
55-
multiplier: 1.0,
56-
constant: 0.0)
57-
58-
let left = NSLayoutConstraint(item: contentView,
59-
attribute: .leading,
60-
relatedBy: .equal,
61-
toItem: self,
62-
attribute: .leading,
63-
multiplier: 1.0,
64-
constant: 0.0)
65-
66-
let bottom = NSLayoutConstraint (item: self,
67-
attribute: .bottom,
68-
relatedBy: .equal,
69-
toItem: contentView,
70-
attribute: .bottom,
71-
multiplier: 1.0,
72-
constant: 0.0)
73-
74-
let right = NSLayoutConstraint(item: self,
75-
attribute: .trailing,
76-
relatedBy: .equal,
77-
toItem: contentView,
78-
attribute: .trailing,
79-
multiplier: 1.0,
80-
constant: 0.0)
81-
8250
contentView.translatesAutoresizingMaskIntoConstraints = false
83-
self.addConstraints([top, left, bottom, right])
51+
NSLayoutConstraint.activate([
52+
contentView.topAnchor.constraint(equalTo: topAnchor),
53+
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
54+
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
55+
contentView.trailingAnchor.constraint(equalTo: trailingAnchor)
56+
])
8457

8558
let bundle = Bundle(for: TabView.self)
8659
let nib = UINib(nibName: TabCollectionCell.cellIdentifier(), bundle: bundle)
@@ -95,23 +68,13 @@ internal class TabView: UIView {
9568
currentBarView.removeFromSuperview()
9669
collectionView.addSubview(currentBarView)
9770
currentBarView.translatesAutoresizingMaskIntoConstraints = false
98-
let top = NSLayoutConstraint(item: currentBarView,
99-
attribute: .top,
100-
relatedBy: .equal,
101-
toItem: collectionView,
102-
attribute: .top,
103-
multiplier: 1.0,
104-
constant: option.tabHeight - currentBarViewHeightConstraint.constant)
105-
106-
let left = NSLayoutConstraint(item: currentBarView,
107-
attribute: .leading,
108-
relatedBy: .equal,
109-
toItem: collectionView,
110-
attribute: .leading,
111-
multiplier: 1.0,
112-
constant: 0.0)
113-
currentBarViewLeftConstraint = left
114-
collectionView.addConstraints([top, left])
71+
currentBarViewLeftConstraint = currentBarView.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor)
72+
NSLayoutConstraint.activate([
73+
currentBarView.topAnchor.constraint(
74+
equalTo: collectionView.topAnchor,
75+
constant: option.tabHeight - currentBarViewHeightConstraint.constant),
76+
currentBarViewLeftConstraint!
77+
])
11578
}
11679

11780
bottomBarViewHeightConstraint.constant = 1.0 / UIScreen.main.scale

TabPageViewController.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@
418418
GCC_WARN_UNUSED_FUNCTION = YES;
419419
GCC_WARN_UNUSED_VARIABLE = YES;
420420
INFOPLIST_FILE = Demo/TabPageViewControllerDemo/Info.plist;
421-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
421+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
422422
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
423423
MTL_ENABLE_DEBUG_INFO = YES;
424424
PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.TabPageViewControllerDemo;
@@ -464,7 +464,7 @@
464464
GCC_WARN_UNUSED_FUNCTION = YES;
465465
GCC_WARN_UNUSED_VARIABLE = YES;
466466
INFOPLIST_FILE = Demo/TabPageViewControllerDemo/Info.plist;
467-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
467+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
468468
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
469469
MTL_ENABLE_DEBUG_INFO = NO;
470470
PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.TabPageViewControllerDemo;
@@ -487,7 +487,7 @@
487487
);
488488
HEADER_SEARCH_PATHS = "$(inherited)";
489489
INFOPLIST_FILE = TabPageViewController.xcodeproj/TabPageViewController_Info.plist;
490-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
490+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
491491
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
492492
OTHER_LDFLAGS = "$(inherited)";
493493
OTHER_SWIFT_FLAGS = "$(inherited)";
@@ -511,7 +511,7 @@
511511
);
512512
HEADER_SEARCH_PATHS = "$(inherited)";
513513
INFOPLIST_FILE = TabPageViewController.xcodeproj/TabPageViewController_Info.plist;
514-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
514+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
515515
LD_RUNPATH_SEARCH_PATHS = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
516516
OTHER_LDFLAGS = "$(inherited)";
517517
OTHER_SWIFT_FLAGS = "$(inherited)";
@@ -567,7 +567,7 @@
567567
DYLIB_INSTALL_NAME_BASE = "@rpath";
568568
ENABLE_NS_ASSERTIONS = YES;
569569
GCC_OPTIMIZATION_LEVEL = 0;
570-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
570+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
571571
ONLY_ACTIVE_ARCH = YES;
572572
OTHER_SWIFT_FLAGS = "-DXcode";
573573
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -589,7 +589,7 @@
589589
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
590590
DYLIB_INSTALL_NAME_BASE = "@rpath";
591591
GCC_OPTIMIZATION_LEVEL = s;
592-
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
592+
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
593593
OTHER_SWIFT_FLAGS = "-DXcode";
594594
PRODUCT_NAME = "$(TARGET_NAME)";
595595
SDKROOT = iphoneos;

0 commit comments

Comments
 (0)