I want to put bordercolor with uiview by swift.

Hi there.

I want to put bordercolor with uiview by swift,but i can't do it on iOS7.


----------------------------

self.layer.borderColor = UIColor.blackColor().CGColor

self.layer.borderWidth = 1.0

---------------------------


This code can be used on iOS8,but it can't be used on iOS7.

In iOS7,i imported 'quartzcore' in my code,i couldn't do it.


what should i do?


Thank you.

Hi shunsuke

Are you seeing a crash with the code you pasted or is it just having no effect? Have you confirmed with the debugger that the code is being executed?

Hello Justin.

Thank you for reprying.

>Are you seeing a crash with the code you pasted or is it just having no effect?

I'm sorry.There is a crash.

Crash log is following.

---------------------------------------------------------------

2015-06-09 13:54:59.860 shotworks_for_ph2_by_swift[43366:607] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x7fc5e04d86c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key borderColor.'

*** First throw call stack:

(

0 CoreFoundation 0x000000010d435495 __exceptionPreprocess + 165

1 libobjc.A.dylib 0x000000010d19499e objc_exception_throw + 43

2 CoreFoundation 0x000000010d4b9919 -[NSException raise] + 9

3 Foundation 0x000000010cd75530 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259

4 UIKit 0x000000010d716569 -[UIView(CALayerDelegate) setValue:forKey:] + 149

5 CoreFoundation 0x000000010d431400 -[NSArray makeObjectsPerformSelector:] + 224

6 UIKit 0x000000010d911893 -[UINib instantiateWithOwner:options:] + 1112

7 UIKit 0x000000010d78044b -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 302

8 UIKit 0x000000010d7806b6 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 44

9 shotworks_for_ph2_by_swift 0x000000010b02bc3b _TFC26shotworks_for_ph2_by_swift13EasySearchVCL9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 1163

10 shotworks_for_ph2_by_swift 0x000000010b02bd2f _TToFC26shotworks_for_ph2_by_swift13EasySearchVCL9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79

11 UIKit 0x000000010d78af8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348

12 UIKit 0x000000010d770d5b -[UITableView _updateVisibleCellsNow:] + 2337

13 UIKit 0x000000010d782721 -[UITableView layoutSubviews] + 207

14 UIKit 0x000000010d716993 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354

15 QuartzCore 0x000000010c7dd802 -[CALayer layoutSublayers] + 151

16 QuartzCore 0x000000010c7d2369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363

17 UIKit 0x000000010d70af70 -[UIView(Hierarchy) layoutBelowIfNeeded] + 521

18 UIKit 0x000000010d7cd7d6 -[UINavigationController _layoutViewController:] + 1077

19 UIKit 0x000000010d7cb0b4 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 395

20 UIKit 0x000000010d6fb68e -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 235

21 UIKit 0x000000010d6fa184 +[UIViewAnimationState popAnimationState] + 281

22 UIKit 0x000000010d991f7a -[UINavigationTransitionView transition:fromView:toView:] + 2504

23 UIKit 0x000000010d9915b0 -[UINavigationTransitionView transition:toView:] + 25

24 UIKit 0x000000010d7ce4d7 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2893

25 UIKit 0x000000010d7ce787 -[UINavigationController _startDeferredTransitionIfNeeded:] + 547

26 UIKit 0x000000010d7cf238 -[UINavigationController __viewWillLayoutSubviews] + 43

27 UIKit 0x000000010d8e9895 -[UILayoutContainerView layoutSubviews] + 202

28 UIKit 0x000000010d716993 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354

29 QuartzCore 0x000000010c7dd802 -[CALayer layoutSublayers] + 151

30 QuartzCore 0x000000010c7d2369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363

31 QuartzCore 0x000000010c7dd736 -[CALayer layoutIfNeeded] + 162

32 UIKit 0x000000010d7bca22 -[UIViewController window:setupWithInterfaceOrientation:] + 264

33 UIKit 0x000000010d6f3cad -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 4360

34 UIKit 0x000000010d6f2b9f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36

35 UIKit 0x000000010d6f2aef -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101

36 UIKit 0x000000010d6f1dfe -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377

37 UIKit 0x000000010d7b070a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147

38 UIKit 0x000000010d6ecb1b -[UIWindow addRootViewControllerViewIfPossible] + 490

39 UIKit 0x000000010d6ecc70 -[UIWindow _setHidden:forced:] + 282

40 UIKit 0x000000010d6f5ffa -[UIWindow makeKeyAndVisible] + 51

41 UIKit 0x000000010d6b1c98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788

42 UIKit 0x000000010d6b5a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660

43 UIKit 0x000000010d6c6d4c -[UIApplication handleEvent:withNewEvent:] + 3189

44 UIKit 0x000000010d6c7216 -[UIApplication sendEvent:] + 79

45 UIKit 0x000000010d6b7086 _UIApplicationHandleEvent + 578

46 GraphicsServices 0x000000010f56c71a _PurpleEventCallback + 762

47 GraphicsServices 0x000000010f56c1e1 PurpleEventCallback + 35

48 CoreFoundation 0x000000010d3b7679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41

49 CoreFoundation 0x000000010d3b744e __CFRunLoopDoSource1 + 478

50 CoreFoundation 0x000000010d3e0903 __CFRunLoopRun + 1939

51 CoreFoundation 0x000000010d3dfd83 CFRunLoopRunSpecific + 467

52 UIKit 0x000000010d6b52e1 -[UIApplication _run] + 609

53 UIKit 0x000000010d6b6e33 UIApplicationMain + 1010

54 shotworks_for_ph2_by_swift 0x000000010b065ea7 main + 135

55 libdyld.dylib 0x000000010edd75c9 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb) bt

* thread #1: tid = 0x19618a, 0x000000010ef04286 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT

frame #0: 0x000000010ef04286 libsystem_kernel.dylib`__pthread_kill + 10

frame #1: 0x000000010eed142f libsystem_pthread.dylib`pthread_kill + 90

frame #2: 0x000000010ec32e59 libsystem_sim_c.dylib`abort + 101

frame #3: 0x000000010ea88bf3 libc++abi.dylib`abort_message + 195

frame #4: 0x000000010eaae51e libc++abi.dylib`default_terminate_handler() + 264

frame #5: 0x000000010d194bfe libobjc.A.dylib`_objc_terminate() + 103

frame #6: 0x000000010eaabe91 libc++abi.dylib`std::__terminate(void (*)()) + 8

frame #7: 0x000000010eaabb3d libc++abi.dylib`__cxa_rethrow + 109

frame #8: 0x000000010d194b11 libobjc.A.dylib`objc_exception_rethrow + 40

frame #9: 0x000000010d3dfe34 CoreFoundation`CFRunLoopRunSpecific + 644

frame #10: 0x000000010d6b52e1 UIKit`-[UIApplication _run] + 609

frame #11: 0x000000010d6b6e33 UIKit`UIApplicationMain + 1010

* frame #12: 0x000000010b065ea7 shotworks_for_ph2_by_swift`main + 135 at AppDelegate.swift:15

frame #13: 0x000000010edd75c9 libdyld.dylib`start + 1

frame #14: 0x000000010edd75c9 libdyld.dylib`start + 1

(lldb)

----------------------------------------------------------------

what should i do?

Did you perhaps try to set the borderColor in interface builders User Defined Runtime Attributes? (3rd tab in the Utilities pane)

Oh!Thank you!

I will try it.

However,if i create uiview without interface builder,What should i do?

Thank you.

I probably resolved this problem.

'User Defined Runtime Attributes' was good clue for me.

I tried the following code by referencing https://www.weheartswift.com/make-awesome-ui-components-ios-8-using-swift-xcode-6/


@IBDesignable

class BorderView: UIView {



@IBInspectable var cornerRadius: CGFloat = 5 {

didSet {

layer.cornerRadius = cornerRadius

layer.masksToBounds = cornerRadius > 0

}

}

@IBInspectable var borderWidth: CGFloat = 1 {

didSet {

layer.borderWidth = borderWidth

}

}

@IBInspectable var borderColor: UIColor? = UIColor.blueColor() {

didSet {

layer.borderColor = borderColor?.CGColor

}

}



}


if i tried above file on another project,it ran correctly.

if i couldn't run correctly another project,i would write it.


Thank you.

Hi.

1 week ago,I putted User Defined Runtime Attributes on interface builder.

It was bad for my application.

It ran correctly after deleting 'User Defined Runtime Attributes'.


Thank you!

I want to put bordercolor with uiview by swift.
 
 
Q