├── .gitignore ├── LICENSE ├── README.md └── UINavigationItem-SXFixSpace ├── UINavigationItem-SXFixSpace.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── UINavigationItem-SXFixSpace ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json ├── Contents.json ├── nav_add.imageset │ ├── Contents.json │ ├── nav_add@2x.png │ └── nav_add@3x.png └── nav_back.imageset │ ├── Contents.json │ ├── nav_back@2x.png │ └── nav_back@3x.png ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── NSObject+SXRuntime.h ├── NSObject+SXRuntime.m ├── UIBarButtonItem+SXCreate.h ├── UIBarButtonItem+SXCreate.m ├── UINavigationController+SXFullScreen.h ├── UINavigationController+SXFullScreen.m ├── UINavigationItem+SXFixSpace.h ├── UINavigationItem+SXFixSpace.m ├── ViewController.h ├── ViewController.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Charles 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iOS-11-UINavigationItem-SXFixSpace 2 | iOS11导航栏按钮的位置偏移问题的解决方案 及iOS11之前的导航栏按钮的位置偏移问题的解决方案 3 | 4 | 5 | 另外还有新的解决方案 6 | https://github.com/spicyShrimp/UINavigation-SXFixSpace 7 | 相较于次方案 8 | 1.不用修改约束(其修改的是系统默认的layoutMargins) 9 | 2.不用必须写在viewWillAppear中(新方案不会因为push和pop导致约束丢失) 10 | 3.仍可以使用系统的set..Items的方法进行多个按钮的设置 11 | 4.实现逻辑更简单 12 | ... 13 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 5699996F1F6FCBC600C690DB /* UINavigationController+SXFullScreen.m in Sources */ = {isa = PBXBuildFile; fileRef = 5699996E1F6FCBC600C690DB /* UINavigationController+SXFullScreen.m */; }; 11 | 56AE73B21F6242730066856D /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 56AE73B11F6242730066856D /* AppDelegate.m */; }; 12 | 56AE73B51F6242730066856D /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 56AE73B41F6242730066856D /* ViewController.m */; }; 13 | 56AE73B81F6242730066856D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 56AE73B61F6242730066856D /* Main.storyboard */; }; 14 | 56AE73BA1F6242730066856D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56AE73B91F6242730066856D /* Assets.xcassets */; }; 15 | 56AE73BD1F6242730066856D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 56AE73BB1F6242730066856D /* LaunchScreen.storyboard */; }; 16 | 56AE73C01F6242730066856D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 56AE73BF1F6242730066856D /* main.m */; }; 17 | 56AE73C81F62429C0066856D /* UINavigationItem+SXFixSpace.m in Sources */ = {isa = PBXBuildFile; fileRef = 56AE73C71F62429C0066856D /* UINavigationItem+SXFixSpace.m */; }; 18 | 56AE73CB1F6242B70066856D /* NSObject+SXRuntime.m in Sources */ = {isa = PBXBuildFile; fileRef = 56AE73C91F6242B60066856D /* NSObject+SXRuntime.m */; }; 19 | 56AE73CE1F62445B0066856D /* UIBarButtonItem+SXCreate.m in Sources */ = {isa = PBXBuildFile; fileRef = 56AE73CD1F62445B0066856D /* UIBarButtonItem+SXCreate.m */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXFileReference section */ 23 | 5699996D1F6FCBC600C690DB /* UINavigationController+SXFullScreen.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UINavigationController+SXFullScreen.h"; sourceTree = ""; }; 24 | 5699996E1F6FCBC600C690DB /* UINavigationController+SXFullScreen.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UINavigationController+SXFullScreen.m"; sourceTree = ""; }; 25 | 56AE73AD1F6242730066856D /* UINavigationItem-SXFixSpace.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "UINavigationItem-SXFixSpace.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 26 | 56AE73B01F6242730066856D /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 27 | 56AE73B11F6242730066856D /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 28 | 56AE73B31F6242730066856D /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 29 | 56AE73B41F6242730066856D /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 30 | 56AE73B71F6242730066856D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 31 | 56AE73B91F6242730066856D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 32 | 56AE73BC1F6242730066856D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 33 | 56AE73BE1F6242730066856D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 34 | 56AE73BF1F6242730066856D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 35 | 56AE73C61F62429C0066856D /* UINavigationItem+SXFixSpace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UINavigationItem+SXFixSpace.h"; sourceTree = ""; }; 36 | 56AE73C71F62429C0066856D /* UINavigationItem+SXFixSpace.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UINavigationItem+SXFixSpace.m"; sourceTree = ""; }; 37 | 56AE73C91F6242B60066856D /* NSObject+SXRuntime.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SXRuntime.m"; sourceTree = ""; }; 38 | 56AE73CA1F6242B70066856D /* NSObject+SXRuntime.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSObject+SXRuntime.h"; sourceTree = ""; }; 39 | 56AE73CC1F62445A0066856D /* UIBarButtonItem+SXCreate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIBarButtonItem+SXCreate.h"; sourceTree = ""; }; 40 | 56AE73CD1F62445B0066856D /* UIBarButtonItem+SXCreate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIBarButtonItem+SXCreate.m"; sourceTree = ""; }; 41 | /* End PBXFileReference section */ 42 | 43 | /* Begin PBXFrameworksBuildPhase section */ 44 | 56AE73AA1F6242730066856D /* Frameworks */ = { 45 | isa = PBXFrameworksBuildPhase; 46 | buildActionMask = 2147483647; 47 | files = ( 48 | ); 49 | runOnlyForDeploymentPostprocessing = 0; 50 | }; 51 | /* End PBXFrameworksBuildPhase section */ 52 | 53 | /* Begin PBXGroup section */ 54 | 56AE73A41F6242730066856D = { 55 | isa = PBXGroup; 56 | children = ( 57 | 56AE73AF1F6242730066856D /* UINavigationItem-SXFixSpace */, 58 | 56AE73AE1F6242730066856D /* Products */, 59 | ); 60 | sourceTree = ""; 61 | }; 62 | 56AE73AE1F6242730066856D /* Products */ = { 63 | isa = PBXGroup; 64 | children = ( 65 | 56AE73AD1F6242730066856D /* UINavigationItem-SXFixSpace.app */, 66 | ); 67 | name = Products; 68 | sourceTree = ""; 69 | }; 70 | 56AE73AF1F6242730066856D /* UINavigationItem-SXFixSpace */ = { 71 | isa = PBXGroup; 72 | children = ( 73 | 5699996D1F6FCBC600C690DB /* UINavigationController+SXFullScreen.h */, 74 | 5699996E1F6FCBC600C690DB /* UINavigationController+SXFullScreen.m */, 75 | 56AE73CC1F62445A0066856D /* UIBarButtonItem+SXCreate.h */, 76 | 56AE73CD1F62445B0066856D /* UIBarButtonItem+SXCreate.m */, 77 | 56AE73CA1F6242B70066856D /* NSObject+SXRuntime.h */, 78 | 56AE73C91F6242B60066856D /* NSObject+SXRuntime.m */, 79 | 56AE73C61F62429C0066856D /* UINavigationItem+SXFixSpace.h */, 80 | 56AE73C71F62429C0066856D /* UINavigationItem+SXFixSpace.m */, 81 | 56AE73B01F6242730066856D /* AppDelegate.h */, 82 | 56AE73B11F6242730066856D /* AppDelegate.m */, 83 | 56AE73B31F6242730066856D /* ViewController.h */, 84 | 56AE73B41F6242730066856D /* ViewController.m */, 85 | 56AE73B61F6242730066856D /* Main.storyboard */, 86 | 56AE73B91F6242730066856D /* Assets.xcassets */, 87 | 56AE73BB1F6242730066856D /* LaunchScreen.storyboard */, 88 | 56AE73BE1F6242730066856D /* Info.plist */, 89 | 56AE73BF1F6242730066856D /* main.m */, 90 | ); 91 | path = "UINavigationItem-SXFixSpace"; 92 | sourceTree = ""; 93 | }; 94 | /* End PBXGroup section */ 95 | 96 | /* Begin PBXNativeTarget section */ 97 | 56AE73AC1F6242730066856D /* UINavigationItem-SXFixSpace */ = { 98 | isa = PBXNativeTarget; 99 | buildConfigurationList = 56AE73C31F6242730066856D /* Build configuration list for PBXNativeTarget "UINavigationItem-SXFixSpace" */; 100 | buildPhases = ( 101 | 56AE73A91F6242730066856D /* Sources */, 102 | 56AE73AA1F6242730066856D /* Frameworks */, 103 | 56AE73AB1F6242730066856D /* Resources */, 104 | ); 105 | buildRules = ( 106 | ); 107 | dependencies = ( 108 | ); 109 | name = "UINavigationItem-SXFixSpace"; 110 | productName = "UINavigationItem-SXFixSpace"; 111 | productReference = 56AE73AD1F6242730066856D /* UINavigationItem-SXFixSpace.app */; 112 | productType = "com.apple.product-type.application"; 113 | }; 114 | /* End PBXNativeTarget section */ 115 | 116 | /* Begin PBXProject section */ 117 | 56AE73A51F6242730066856D /* Project object */ = { 118 | isa = PBXProject; 119 | attributes = { 120 | LastUpgradeCheck = 0900; 121 | ORGANIZATIONNAME = None; 122 | TargetAttributes = { 123 | 56AE73AC1F6242730066856D = { 124 | CreatedOnToolsVersion = 9.0; 125 | ProvisioningStyle = Automatic; 126 | }; 127 | }; 128 | }; 129 | buildConfigurationList = 56AE73A81F6242730066856D /* Build configuration list for PBXProject "UINavigationItem-SXFixSpace" */; 130 | compatibilityVersion = "Xcode 8.0"; 131 | developmentRegion = en; 132 | hasScannedForEncodings = 0; 133 | knownRegions = ( 134 | en, 135 | Base, 136 | ); 137 | mainGroup = 56AE73A41F6242730066856D; 138 | productRefGroup = 56AE73AE1F6242730066856D /* Products */; 139 | projectDirPath = ""; 140 | projectRoot = ""; 141 | targets = ( 142 | 56AE73AC1F6242730066856D /* UINavigationItem-SXFixSpace */, 143 | ); 144 | }; 145 | /* End PBXProject section */ 146 | 147 | /* Begin PBXResourcesBuildPhase section */ 148 | 56AE73AB1F6242730066856D /* Resources */ = { 149 | isa = PBXResourcesBuildPhase; 150 | buildActionMask = 2147483647; 151 | files = ( 152 | 56AE73BD1F6242730066856D /* LaunchScreen.storyboard in Resources */, 153 | 56AE73BA1F6242730066856D /* Assets.xcassets in Resources */, 154 | 56AE73B81F6242730066856D /* Main.storyboard in Resources */, 155 | ); 156 | runOnlyForDeploymentPostprocessing = 0; 157 | }; 158 | /* End PBXResourcesBuildPhase section */ 159 | 160 | /* Begin PBXSourcesBuildPhase section */ 161 | 56AE73A91F6242730066856D /* Sources */ = { 162 | isa = PBXSourcesBuildPhase; 163 | buildActionMask = 2147483647; 164 | files = ( 165 | 56AE73B51F6242730066856D /* ViewController.m in Sources */, 166 | 56AE73CE1F62445B0066856D /* UIBarButtonItem+SXCreate.m in Sources */, 167 | 56AE73C01F6242730066856D /* main.m in Sources */, 168 | 56AE73C81F62429C0066856D /* UINavigationItem+SXFixSpace.m in Sources */, 169 | 56AE73B21F6242730066856D /* AppDelegate.m in Sources */, 170 | 5699996F1F6FCBC600C690DB /* UINavigationController+SXFullScreen.m in Sources */, 171 | 56AE73CB1F6242B70066856D /* NSObject+SXRuntime.m in Sources */, 172 | ); 173 | runOnlyForDeploymentPostprocessing = 0; 174 | }; 175 | /* End PBXSourcesBuildPhase section */ 176 | 177 | /* Begin PBXVariantGroup section */ 178 | 56AE73B61F6242730066856D /* Main.storyboard */ = { 179 | isa = PBXVariantGroup; 180 | children = ( 181 | 56AE73B71F6242730066856D /* Base */, 182 | ); 183 | name = Main.storyboard; 184 | sourceTree = ""; 185 | }; 186 | 56AE73BB1F6242730066856D /* LaunchScreen.storyboard */ = { 187 | isa = PBXVariantGroup; 188 | children = ( 189 | 56AE73BC1F6242730066856D /* Base */, 190 | ); 191 | name = LaunchScreen.storyboard; 192 | sourceTree = ""; 193 | }; 194 | /* End PBXVariantGroup section */ 195 | 196 | /* Begin XCBuildConfiguration section */ 197 | 56AE73C11F6242730066856D /* Debug */ = { 198 | isa = XCBuildConfiguration; 199 | buildSettings = { 200 | ALWAYS_SEARCH_USER_PATHS = NO; 201 | CLANG_ANALYZER_NONNULL = YES; 202 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 203 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 204 | CLANG_CXX_LIBRARY = "libc++"; 205 | CLANG_ENABLE_MODULES = YES; 206 | CLANG_ENABLE_OBJC_ARC = YES; 207 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 208 | CLANG_WARN_BOOL_CONVERSION = YES; 209 | CLANG_WARN_COMMA = YES; 210 | CLANG_WARN_CONSTANT_CONVERSION = YES; 211 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 212 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 213 | CLANG_WARN_EMPTY_BODY = YES; 214 | CLANG_WARN_ENUM_CONVERSION = YES; 215 | CLANG_WARN_INFINITE_RECURSION = YES; 216 | CLANG_WARN_INT_CONVERSION = YES; 217 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 218 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 219 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 220 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 221 | CLANG_WARN_STRICT_PROTOTYPES = YES; 222 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 223 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 224 | CLANG_WARN_UNREACHABLE_CODE = YES; 225 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 226 | CODE_SIGN_IDENTITY = "iPhone Developer"; 227 | COPY_PHASE_STRIP = NO; 228 | DEBUG_INFORMATION_FORMAT = dwarf; 229 | ENABLE_STRICT_OBJC_MSGSEND = YES; 230 | ENABLE_TESTABILITY = YES; 231 | GCC_C_LANGUAGE_STANDARD = gnu11; 232 | GCC_DYNAMIC_NO_PIC = NO; 233 | GCC_NO_COMMON_BLOCKS = YES; 234 | GCC_OPTIMIZATION_LEVEL = 0; 235 | GCC_PREPROCESSOR_DEFINITIONS = ( 236 | "DEBUG=1", 237 | "$(inherited)", 238 | ); 239 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 240 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 241 | GCC_WARN_UNDECLARED_SELECTOR = YES; 242 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 243 | GCC_WARN_UNUSED_FUNCTION = YES; 244 | GCC_WARN_UNUSED_VARIABLE = YES; 245 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 246 | MTL_ENABLE_DEBUG_INFO = YES; 247 | ONLY_ACTIVE_ARCH = YES; 248 | SDKROOT = iphoneos; 249 | }; 250 | name = Debug; 251 | }; 252 | 56AE73C21F6242730066856D /* Release */ = { 253 | isa = XCBuildConfiguration; 254 | buildSettings = { 255 | ALWAYS_SEARCH_USER_PATHS = NO; 256 | CLANG_ANALYZER_NONNULL = YES; 257 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 258 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 259 | CLANG_CXX_LIBRARY = "libc++"; 260 | CLANG_ENABLE_MODULES = YES; 261 | CLANG_ENABLE_OBJC_ARC = YES; 262 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 263 | CLANG_WARN_BOOL_CONVERSION = YES; 264 | CLANG_WARN_COMMA = YES; 265 | CLANG_WARN_CONSTANT_CONVERSION = YES; 266 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 267 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 268 | CLANG_WARN_EMPTY_BODY = YES; 269 | CLANG_WARN_ENUM_CONVERSION = YES; 270 | CLANG_WARN_INFINITE_RECURSION = YES; 271 | CLANG_WARN_INT_CONVERSION = YES; 272 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 273 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 274 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 275 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 276 | CLANG_WARN_STRICT_PROTOTYPES = YES; 277 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 278 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 279 | CLANG_WARN_UNREACHABLE_CODE = YES; 280 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 281 | CODE_SIGN_IDENTITY = "iPhone Developer"; 282 | COPY_PHASE_STRIP = NO; 283 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 284 | ENABLE_NS_ASSERTIONS = NO; 285 | ENABLE_STRICT_OBJC_MSGSEND = YES; 286 | GCC_C_LANGUAGE_STANDARD = gnu11; 287 | GCC_NO_COMMON_BLOCKS = YES; 288 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 289 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 290 | GCC_WARN_UNDECLARED_SELECTOR = YES; 291 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 292 | GCC_WARN_UNUSED_FUNCTION = YES; 293 | GCC_WARN_UNUSED_VARIABLE = YES; 294 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 295 | MTL_ENABLE_DEBUG_INFO = NO; 296 | SDKROOT = iphoneos; 297 | VALIDATE_PRODUCT = YES; 298 | }; 299 | name = Release; 300 | }; 301 | 56AE73C41F6242730066856D /* Debug */ = { 302 | isa = XCBuildConfiguration; 303 | buildSettings = { 304 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 305 | CODE_SIGN_STYLE = Automatic; 306 | INFOPLIST_FILE = "UINavigationItem-SXFixSpace/Info.plist"; 307 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 308 | PRODUCT_BUNDLE_IDENTIFIER = "com.charles.UINavigationItem-SXFixSpace"; 309 | PRODUCT_NAME = "$(TARGET_NAME)"; 310 | TARGETED_DEVICE_FAMILY = "1,2"; 311 | }; 312 | name = Debug; 313 | }; 314 | 56AE73C51F6242730066856D /* Release */ = { 315 | isa = XCBuildConfiguration; 316 | buildSettings = { 317 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 318 | CODE_SIGN_STYLE = Automatic; 319 | INFOPLIST_FILE = "UINavigationItem-SXFixSpace/Info.plist"; 320 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 321 | PRODUCT_BUNDLE_IDENTIFIER = "com.charles.UINavigationItem-SXFixSpace"; 322 | PRODUCT_NAME = "$(TARGET_NAME)"; 323 | TARGETED_DEVICE_FAMILY = "1,2"; 324 | }; 325 | name = Release; 326 | }; 327 | /* End XCBuildConfiguration section */ 328 | 329 | /* Begin XCConfigurationList section */ 330 | 56AE73A81F6242730066856D /* Build configuration list for PBXProject "UINavigationItem-SXFixSpace" */ = { 331 | isa = XCConfigurationList; 332 | buildConfigurations = ( 333 | 56AE73C11F6242730066856D /* Debug */, 334 | 56AE73C21F6242730066856D /* Release */, 335 | ); 336 | defaultConfigurationIsVisible = 0; 337 | defaultConfigurationName = Release; 338 | }; 339 | 56AE73C31F6242730066856D /* Build configuration list for PBXNativeTarget "UINavigationItem-SXFixSpace" */ = { 340 | isa = XCConfigurationList; 341 | buildConfigurations = ( 342 | 56AE73C41F6242730066856D /* Debug */, 343 | 56AE73C51F6242730066856D /* Release */, 344 | ); 345 | defaultConfigurationIsVisible = 0; 346 | defaultConfigurationName = Release; 347 | }; 348 | /* End XCConfigurationList section */ 349 | }; 350 | rootObject = 56AE73A51F6242730066856D /* Project object */; 351 | } 352 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_add.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "nav_add@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "nav_add@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_add.imageset/nav_add@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spicyShrimp/iOS-11-UINavigationItem-SXFixSpace/1fd9cd6c00f0e49756b4bb11f2cb81bac9995068/UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_add.imageset/nav_add@2x.png -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_add.imageset/nav_add@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spicyShrimp/iOS-11-UINavigationItem-SXFixSpace/1fd9cd6c00f0e49756b4bb11f2cb81bac9995068/UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_add.imageset/nav_add@3x.png -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_back.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "nav_back@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "filename" : "nav_back@3x.png", 15 | "scale" : "3x" 16 | } 17 | ], 18 | "info" : { 19 | "version" : 1, 20 | "author" : "xcode" 21 | } 22 | } -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_back.imageset/nav_back@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spicyShrimp/iOS-11-UINavigationItem-SXFixSpace/1fd9cd6c00f0e49756b4bb11f2cb81bac9995068/UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_back.imageset/nav_back@2x.png -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_back.imageset/nav_back@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spicyShrimp/iOS-11-UINavigationItem-SXFixSpace/1fd9cd6c00f0e49756b4bb11f2cb81bac9995068/UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Assets.xcassets/nav_back.imageset/nav_back@3x.png -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/NSObject+SXRuntime.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+SXRuntime.h 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface NSObject (SXRuntime) 13 | 14 | /** 15 | swizzle 类方法 16 | 17 | @param oriSel 原有的方法 18 | @param swiSel swizzle的方法 19 | */ 20 | + (void)swizzleClassMethodWithOriginSel:(SEL)oriSel swizzledSel:(SEL)swiSel; 21 | 22 | /** 23 | swizzle 实例方法 24 | 25 | @param oriSel 原有的方法 26 | @param swiSel swizzle的方法 27 | */ 28 | + (void)swizzleInstanceMethodWithOriginSel:(SEL)oriSel swizzledSel:(SEL)swiSel; 29 | 30 | /** 31 | 判断方法是否在子类里override了 32 | 33 | @param cls 传入要判断的Class 34 | @param sel 传入要判断的Selector 35 | @return 返回判断是否被重载的结果 36 | */ 37 | - (BOOL)isMethodOverride:(Class)cls selector:(SEL)sel; 38 | 39 | /** 40 | 判断当前类是否在主bundle里 41 | 42 | @param cls 出入类 43 | @return 返回判断结果 44 | */ 45 | + (BOOL)isMainBundleClass:(Class)cls; 46 | 47 | /** 48 | 动态创建绑定selector的类 49 | tip:每当无法找到selectorcrash转发过来的所有selector都会追加到当前Class上 50 | 51 | @param aSelector 传入selector 52 | @return 返回创建的类 53 | */ 54 | + (Class)addMethodToStubClass:(SEL)aSelector; 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/NSObject+SXRuntime.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+SXRuntime.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import "NSObject+SXRuntime.h" 10 | 11 | char * const kProtectCrashProtectorName = "kProtectCrashProtector"; 12 | 13 | void ProtectCrashProtected(id self, SEL sel) { 14 | } 15 | 16 | @implementation NSObject (SXRuntime) 17 | 18 | // MARK: Util 19 | + (void)swizzleClassMethodWithOriginSel:(SEL)oriSel swizzledSel:(SEL)swiSel { 20 | Class cls = object_getClass(self); 21 | 22 | Method originAddObserverMethod = class_getClassMethod(cls, oriSel); 23 | Method swizzledAddObserverMethod = class_getClassMethod(cls, swiSel); 24 | 25 | [self swizzleMethodWithOriginSel:oriSel oriMethod:originAddObserverMethod swizzledSel:swiSel swizzledMethod:swizzledAddObserverMethod class:cls]; 26 | } 27 | 28 | + (void)swizzleInstanceMethodWithOriginSel:(SEL)oriSel swizzledSel:(SEL)swiSel { 29 | Method originAddObserverMethod = class_getInstanceMethod(self, oriSel); 30 | Method swizzledAddObserverMethod = class_getInstanceMethod(self, swiSel); 31 | 32 | [self swizzleMethodWithOriginSel:oriSel oriMethod:originAddObserverMethod swizzledSel:swiSel swizzledMethod:swizzledAddObserverMethod class:self]; 33 | } 34 | 35 | + (void)swizzleMethodWithOriginSel:(SEL)oriSel 36 | oriMethod:(Method)oriMethod 37 | swizzledSel:(SEL)swizzledSel 38 | swizzledMethod:(Method)swizzledMethod 39 | class:(Class)cls { 40 | BOOL didAddMethod = class_addMethod(cls, oriSel, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); 41 | 42 | if (didAddMethod) { 43 | class_replaceMethod(cls, swizzledSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod)); 44 | } else { 45 | method_exchangeImplementations(oriMethod, swizzledMethod); 46 | } 47 | } 48 | 49 | + (Class)addMethodToStubClass:(SEL)aSelector { 50 | Class ProtectCrashProtector = objc_getClass(kProtectCrashProtectorName); 51 | 52 | if (!ProtectCrashProtector) { 53 | ProtectCrashProtector = objc_allocateClassPair([NSObject class], kProtectCrashProtectorName, sizeof([NSObject class])); 54 | objc_registerClassPair(ProtectCrashProtector); 55 | } 56 | 57 | class_addMethod(ProtectCrashProtector, aSelector, (IMP)ProtectCrashProtected, "v@:"); 58 | return ProtectCrashProtector; 59 | } 60 | 61 | - (BOOL)isMethodOverride:(Class)cls selector:(SEL)sel { 62 | IMP clsIMP = class_getMethodImplementation(cls, sel); 63 | IMP superClsIMP = class_getMethodImplementation([cls superclass], sel); 64 | 65 | return clsIMP != superClsIMP; 66 | } 67 | 68 | + (BOOL)isMainBundleClass:(Class)cls { 69 | return cls && [[NSBundle bundleForClass:cls] isEqual:[NSBundle mainBundle]]; 70 | } 71 | 72 | @end; 73 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/UIBarButtonItem+SXCreate.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIBarButtonItem+SXCreate.h 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIBarButtonItem (SXCreate) 12 | 13 | /** 14 | 根据图片生成UIBarButtonItem 15 | 16 | @param target target对象 17 | @param action 响应方法 18 | @param image image 19 | @return 生成的UIBarButtonItem 20 | */ 21 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image; 22 | /** 23 | 根据图片生成UIBarButtonItem 24 | 25 | @param target target对象 26 | @param action 响应方法 27 | @param image image 28 | @param imageEdgeInsets 图片偏移 29 | @return 生成的UIBarButtonItem 30 | */ 31 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets; 32 | 33 | /** 34 | 根据图片生成UIBarButtonItem 35 | 36 | @param target target对象 37 | @param action 响应方法 38 | @param nomalImage nomalImage 39 | @param higeLightedImage higeLightedImage 40 | @param imageEdgeInsets 图片偏移 41 | @return 生成的UIBarButtonItem 42 | */ 43 | +(UIBarButtonItem *)itemWithTarget:(id)target 44 | action:(SEL)action 45 | nomalImage:(UIImage *)nomalImage 46 | higeLightedImage:(UIImage *)higeLightedImage 47 | imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets; 48 | 49 | 50 | /** 51 | 根据文字生成UIBarButtonItem 52 | 53 | @param target target对象 54 | @param action 响应方法 55 | @param title title 56 | */ 57 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title; 58 | 59 | /** 60 | 根据文字生成UIBarButtonItem 61 | 62 | @param target target对象 63 | @param action 响应方法 64 | @param title title 65 | @param titleEdgeInsets 文字偏移 66 | @return 生成的UIBarButtonItem 67 | */ 68 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets; 69 | 70 | /** 71 | 根据文字生成UIBarButtonItem 72 | 73 | @param target target对象 74 | @param action 响应方法 75 | @param title title 76 | @param font font 77 | @param titleColor 字体颜色 78 | @param highlightedColor 高亮颜色 79 | @param titleEdgeInsets 文字偏移 80 | @return 生成的UIBarButtonItem 81 | */ 82 | +(UIBarButtonItem *)itemWithTarget:(id)target 83 | action:(SEL)action 84 | title:(NSString *)title 85 | font:(UIFont *)font 86 | titleColor:(UIColor *)titleColor 87 | highlightedColor:(UIColor *)highlightedColor 88 | titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets; 89 | 90 | 91 | /** 92 | 用作修正位置的UIBarButtonItem 93 | 94 | @param width 修正宽度 95 | @return 修正位置的UIBarButtonItem 96 | */ 97 | +(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width; 98 | 99 | @end 100 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/UIBarButtonItem+SXCreate.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIBarButtonItem+SXCreate.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import "UIBarButtonItem+SXCreate.h" 10 | 11 | @implementation UIBarButtonItem (SXCreate) 12 | 13 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image { 14 | return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:UIEdgeInsetsZero]; 15 | } 16 | 17 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(UIImage *)image imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets { 18 | return [self itemWithTarget:target action:action nomalImage:image higeLightedImage:nil imageEdgeInsets:imageEdgeInsets]; 19 | } 20 | 21 | +(UIBarButtonItem *)itemWithTarget:(id)target 22 | action:(SEL)action 23 | nomalImage:(UIImage *)nomalImage 24 | higeLightedImage:(UIImage *)higeLightedImage 25 | imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets { 26 | 27 | UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 28 | [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 29 | 30 | [button setImage:[nomalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; 31 | if (higeLightedImage) { 32 | [button setImage:higeLightedImage forState:UIControlStateHighlighted]; 33 | } 34 | [button sizeToFit]; 35 | if (button.bounds.size.width < 40) { 36 | CGFloat width = 40 / button.bounds.size.height * button.bounds.size.width; 37 | button.bounds = CGRectMake(0, 0, width, 40); 38 | } 39 | button.imageEdgeInsets = imageEdgeInsets; 40 | return [[UIBarButtonItem alloc] initWithCustomView:button]; 41 | 42 | } 43 | 44 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title { 45 | return [self itemWithTarget:target action:action title:title font:nil titleColor:nil highlightedColor:nil titleEdgeInsets:UIEdgeInsetsZero]; 46 | } 47 | 48 | +(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action title:(NSString *)title titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets { 49 | return [self itemWithTarget:target action:action title:title font:nil titleColor:nil highlightedColor:nil titleEdgeInsets:titleEdgeInsets]; 50 | } 51 | 52 | +(UIBarButtonItem *)itemWithTarget:(id)target 53 | action:(SEL)action 54 | title:(NSString *)title 55 | font:(UIFont *)font 56 | titleColor:(UIColor *)titleColor 57 | highlightedColor:(UIColor *)highlightedColor 58 | titleEdgeInsets:(UIEdgeInsets)titleEdgeInsets { 59 | 60 | UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 61 | [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 62 | 63 | [button setTitle:title forState:UIControlStateNormal]; 64 | button.titleLabel.font = font?font:nil; 65 | [button setTitleColor:titleColor?titleColor:[UIColor blackColor] forState:UIControlStateNormal]; 66 | [button setTitleColor:highlightedColor?highlightedColor:nil forState:UIControlStateHighlighted]; 67 | 68 | [button sizeToFit]; 69 | if (button.bounds.size.width < 40) { 70 | CGFloat width = 40 / button.bounds.size.height * button.bounds.size.width; 71 | button.bounds = CGRectMake(0, 0, width, 40); 72 | } 73 | button.titleEdgeInsets = titleEdgeInsets; 74 | return [[UIBarButtonItem alloc] initWithCustomView:button]; 75 | } 76 | 77 | +(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width { 78 | 79 | UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 80 | fixedSpace.width = width; 81 | return fixedSpace; 82 | } 83 | 84 | @end 85 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/UINavigationController+SXFullScreen.h: -------------------------------------------------------------------------------- 1 | // 2 | // UINavigationController+SXFullScreen.h 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIViewController (SXFullScreen) 12 | 13 | /** 14 | 禁止右滑返回属性 15 | */ 16 | @property (nonatomic, assign)BOOL sx_disableInteractivePop; 17 | 18 | @end 19 | 20 | @interface UINavigationController (SXFullScreen) 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/UINavigationController+SXFullScreen.m: -------------------------------------------------------------------------------- 1 | // 2 | // UINavigationController+SXFullScreen.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import "UINavigationController+SXFullScreen.h" 10 | #import "NSObject+SXRuntime.h" 11 | 12 | @implementation UIViewController (SXFullScreen) 13 | 14 | -(BOOL)sx_disableInteractivePop{ 15 | return [objc_getAssociatedObject(self, @selector(sx_disableInteractivePop)) boolValue]; 16 | } 17 | 18 | -(void)setSx_disableInteractivePop:(BOOL)sx_disableInteractivePop{ 19 | objc_setAssociatedObject(self, @selector(sx_disableInteractivePop), @(sx_disableInteractivePop), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 20 | } 21 | 22 | @end 23 | 24 | @interface UINavigationController() 25 | 26 | @end 27 | 28 | @implementation UINavigationController (SXFullScreen) 29 | 30 | + (void)load { 31 | static dispatch_once_t onceToken; 32 | dispatch_once(&onceToken, ^{ 33 | [self swizzleInstanceMethodWithOriginSel:@selector(viewDidLoad) 34 | swizzledSel:@selector(sx_viewDidLoad)]; 35 | }); 36 | } 37 | 38 | -(void)sx_viewDidLoad { 39 | //接替系统滑动返回手势 40 | NSArray *internalTargets = [self.interactivePopGestureRecognizer valueForKey:@"targets"]; 41 | id internalTarget = [internalTargets.firstObject valueForKey:@"target"]; 42 | SEL handler = NSSelectorFromString(@"handleNavigationTransition:"); 43 | 44 | UIPanGestureRecognizer * fullScreenGesture = [[UIPanGestureRecognizer alloc]initWithTarget:internalTarget action:handler]; 45 | fullScreenGesture.delegate = self; 46 | fullScreenGesture.maximumNumberOfTouches = 1; 47 | 48 | UIView *targetView = self.interactivePopGestureRecognizer.view; 49 | [targetView addGestureRecognizer:fullScreenGesture]; 50 | 51 | [self.interactivePopGestureRecognizer setEnabled:NO]; 52 | 53 | [self sx_viewDidLoad]; 54 | } 55 | 56 | /** 57 | 全屏滑动返回判断 58 | 59 | @param gestureRecognizer 手势 60 | @return 是否响应 61 | */ 62 | - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer { 63 | if (self.topViewController.sx_disableInteractivePop) { 64 | return NO; 65 | } 66 | 67 | if ([gestureRecognizer translationInView:gestureRecognizer.view].x <= 0) { 68 | return NO; 69 | } 70 | 71 | if ([[self valueForKey:@"_isTransitioning"] boolValue]) { 72 | return NO; 73 | } 74 | 75 | return (self.childViewControllers.count != 1); 76 | } 77 | 78 | //修复有水平方向滚动的ScrollView时边缘返回手势失效的问题 79 | -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 80 | return (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && [otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]); 81 | } 82 | 83 | @end 84 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/UINavigationItem+SXFixSpace.h: -------------------------------------------------------------------------------- 1 | // 2 | // UINavigationItem+SXFixSpace.h 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UINavigationItem (SXFixSpace) 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/UINavigationItem+SXFixSpace.m: -------------------------------------------------------------------------------- 1 | // 2 | // UINavigationItem+SXFixSpace.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import "UINavigationItem+SXFixSpace.h" 10 | #import "NSObject+SXRuntime.h" 11 | #import 12 | 13 | typedef NS_ENUM(NSInteger, SXBarViewPosition) { 14 | SXBarViewPositionLeft, 15 | SXBarViewPositionRight 16 | }; 17 | 18 | @interface BarView : UIView 19 | @property (nonatomic, assign) SXBarViewPosition position; 20 | @property (nonatomic, assign) BOOL applied; 21 | @end 22 | 23 | @implementation BarView 24 | 25 | - (void)layoutSubviews { 26 | [super layoutSubviews]; 27 | if (self.applied || [[[UIDevice currentDevice] systemVersion] floatValue] < 11) return; 28 | UIView *view = self; 29 | while (![view isKindOfClass:UINavigationBar.class] && view.superview) { 30 | view = [view superview]; 31 | if ([view isKindOfClass:UIStackView.class] && view.superview) { 32 | if (self.position == SXBarViewPositionLeft) { 33 | for (NSLayoutConstraint *constraint in view.superview.constraints) { 34 | if (([constraint.firstItem isKindOfClass:UILayoutGuide.class] && 35 | constraint.firstAttribute == NSLayoutAttributeTrailing)) { 36 | [view.superview removeConstraint:constraint]; 37 | } 38 | } 39 | [view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view 40 | attribute:NSLayoutAttributeLeading 41 | relatedBy:NSLayoutRelationEqual 42 | toItem:view.superview 43 | attribute:NSLayoutAttributeLeading 44 | multiplier:1.0 45 | constant:0]]; 46 | self.applied = YES; 47 | } else if (self.position == SXBarViewPositionRight) { 48 | for (NSLayoutConstraint *constraint in view.superview.constraints) { 49 | if (([constraint.firstItem isKindOfClass:UILayoutGuide.class] && 50 | constraint.firstAttribute == NSLayoutAttributeLeading)) { 51 | [view.superview removeConstraint:constraint]; 52 | } 53 | } 54 | [view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view 55 | attribute:NSLayoutAttributeTrailing 56 | relatedBy:NSLayoutRelationEqual 57 | toItem:view.superview 58 | attribute:NSLayoutAttributeTrailing 59 | multiplier:1.0 60 | constant:0]]; 61 | self.applied = YES; 62 | } 63 | break; 64 | } 65 | } 66 | } 67 | 68 | @end 69 | 70 | @implementation UINavigationItem (SXFixSpace) 71 | 72 | +(void)load { 73 | [self swizzleInstanceMethodWithOriginSel:@selector(setLeftBarButtonItem:) 74 | swizzledSel:@selector(sx_setLeftBarButtonItem:)]; 75 | [self swizzleInstanceMethodWithOriginSel:@selector(setRightBarButtonItem:) 76 | swizzledSel:@selector(sx_setRightBarButtonItem:)]; 77 | } 78 | 79 | -(void)sx_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem{ 80 | if (leftBarButtonItem.customView) { 81 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11) { 82 | UIView *customView = leftBarButtonItem.customView; 83 | BarView *barView = [[BarView alloc]initWithFrame:customView.bounds]; 84 | [barView addSubview:customView]; 85 | customView.center = barView.center; 86 | [barView setPosition:SXBarViewPositionLeft]; 87 | [self setLeftBarButtonItems:nil]; 88 | [self sx_setLeftBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:barView]]; 89 | }else { 90 | [self sx_setLeftBarButtonItem:nil]; 91 | [self setLeftBarButtonItems:@[[self fixedSpaceWithWidth:-20], leftBarButtonItem]]; 92 | } 93 | }else { 94 | [self setLeftBarButtonItems:nil]; 95 | [self sx_setLeftBarButtonItem:leftBarButtonItem]; 96 | } 97 | } 98 | 99 | -(void)sx_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem{ 100 | if (rightBarButtonItem.customView) { 101 | if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11) { 102 | UIView *customView = rightBarButtonItem.customView; 103 | BarView *barView = [[BarView alloc]initWithFrame:customView.bounds]; 104 | [barView addSubview:customView]; 105 | customView.center = barView.center; 106 | [barView setPosition:SXBarViewPositionRight]; 107 | [self setRightBarButtonItems:nil]; 108 | [self sx_setRightBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:barView]]; 109 | } else { 110 | [self sx_setRightBarButtonItem:nil]; 111 | [self setRightBarButtonItems:@[[self fixedSpaceWithWidth:-20], rightBarButtonItem]]; 112 | } 113 | }else { 114 | [self setRightBarButtonItems:nil]; 115 | [self sx_setRightBarButtonItem:rightBarButtonItem]; 116 | } 117 | } 118 | 119 | -(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width { 120 | UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 121 | target:nil 122 | action:nil]; 123 | fixedSpace.width = width; 124 | return fixedSpace; 125 | } 126 | 127 | @end 128 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "UIBarButtonItem+SXCreate.h" 11 | 12 | @interface ViewController () 13 | 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view, typically from a nib. 21 | self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 22 | self.view.backgroundColor = [UIColor whiteColor]; 23 | 24 | if (self.navigationController.viewControllers.count % 2) { 25 | UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)]; 26 | view.backgroundColor = [UIColor redColor]; 27 | self.navigationItem.titleView = view; 28 | } else { 29 | self.navigationItem.title = [@(self.navigationController.viewControllers.count) stringValue]; 30 | } 31 | 32 | } 33 | 34 | -(void)viewWillAppear:(BOOL)animated{ 35 | [super viewWillAppear:animated]; 36 | [self configBarItem]; 37 | } 38 | 39 | -(void)configBarItem{ 40 | if (self.navigationController.viewControllers.count % 2) { 41 | //单个按钮,这里增加了快捷创建的方式 42 | self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(pushAction) image:[UIImage imageNamed:@"nav_add"]]; 43 | } else { 44 | //多个按钮(或者其他任意控件比如segment|slider.....)可以使用自定义视图的方式,这里没有添加快捷方式,使用的是普通的创建方法 45 | //如有需要,可自行根据需求添加快捷创建多个按钮的方法 46 | UIView *barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 40)]; 47 | 48 | UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 49 | btn1.frame = CGRectMake(0, 0, 40, 40); 50 | [btn1 setImage:[UIImage imageNamed:@"nav_add"] forState:UIControlStateNormal]; 51 | [btn1 addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside]; 52 | [barView addSubview:btn1]; 53 | 54 | UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; 55 | btn2.frame = CGRectMake(40, 0, 40, 40); 56 | [btn2 setImage:[UIImage imageNamed:@"nav_add"] forState:UIControlStateNormal]; 57 | [btn2 addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside]; 58 | [barView addSubview:btn2]; 59 | 60 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:barView]; 61 | } 62 | if (self.navigationController.viewControllers.count > 1) { 63 | self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(popAction) image:[UIImage imageNamed:@"nav_back"]]; 64 | } 65 | } 66 | 67 | -(void)pushAction{ 68 | [self.navigationController pushViewController:[ViewController new] animated:YES]; 69 | } 70 | 71 | -(void)popAction{ 72 | [self.navigationController popViewControllerAnimated:YES]; 73 | } 74 | 75 | - (void)didReceiveMemoryWarning { 76 | [super didReceiveMemoryWarning]; 77 | // Dispose of any resources that can be recreated. 78 | } 79 | 80 | 81 | @end 82 | -------------------------------------------------------------------------------- /UINavigationItem-SXFixSpace/UINavigationItem-SXFixSpace/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // UINavigationItem-SXFixSpace 4 | // 5 | // Created by charles on 2017/9/8. 6 | // Copyright © 2017年 None. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------