modal segue slow first time when triggered - ios

I've created a modal segue to a viewController, however when i trigger this segue, there seem to be some delay when viewing the viewController. All i have in the viewController is in the viewDidLoad. what could course this slow modal segue?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Caption".uppercaseString
self.view.backgroundColor = UIColor(hexString: "#0B1E30")
self.navigationController?.navigationBar.barTintColor = UIColor(gradientStyle: UIGradientStyle.LeftToRight, withFrame: CGRectMake(0, 0, self.view.frame.width, (self.navigationController?.navigationBar.frame.height)! + 20), andColors: [UIColor(hexString: "#7F5CE6"), UIColor(hexString: "#9D8FE2")])
let closeButton = UIBarButtonItem(image: UIImage(named: "Cross"), style: UIBarButtonItemStyle.Done, target: self, action: "dismissController")
self.navigationItem.leftBarButtonItem = closeButton
textView?.text = "Add caption (optional)"
textView?.becomeFirstResponder()
textView?.keyboardAppearance = UIKeyboardAppearance.Dark
textView?.font = UIFont(name: "Montserrat-Light", size: 13)
textView?.textColor = UIColor(hexString: "#363636")
capturedImageView = UIImageView(image: capturedPhoto)
capturedImageView?.frame = CGRectMake(8,10, 85, 85)
let path = UIBezierPath(rect: CGRectMake(8, 10, 85, 85))
textView?.textContainer.exclusionPaths = [path]
textView?.addSubview(capturedImageView!)
facebookButton?.tag = 1
facebookButton!.adjustsImageWhenHighlighted = false
facebookButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
facebookButton?.tintColor = UIColor.whiteColor()
facebookButton?.backgroundColor = UIColor(hexString: "#395798")
facebookButton?.contentMode = UIViewContentMode.Center
facebookButton?.setTitle("", forState: UIControlState.Normal)
let fBorder = CALayer()
let width = CGFloat(1.0)
fBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
fBorder.frame = CGRect(x: 0, y: facebookButton!.frame.size.height - width, width: facebookButton!.frame.size.width, height: facebookButton!.frame.size.height)
fBorder.borderWidth = width
facebookButton!.layer.addSublayer(fBorder)
facebookButton!.layer.masksToBounds = true
twitterButton?.tag = 2
twitterButton!.adjustsImageWhenHighlighted = false
twitterButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
twitterButton?.contentMode = UIViewContentMode.Center
twitterButton?.tintColor = UIColor.whiteColor()
twitterButton?.setTitle("", forState: UIControlState.Normal)
let tBorder = CALayer()
tBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
tBorder.frame = CGRect(x: 0, y: twitterButton!.frame.size.height - width, width: twitterButton!.frame.size.width, height: twitterButton!.frame.size.height)
tBorder.borderWidth = width
twitterButton!.layer.addSublayer(tBorder)
twitterButton!.layer.masksToBounds = true
let tRightBorder = CALayer()
tRightBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
tRightBorder.frame = CGRect(x: 0, y: 0, width: width, height: twitterButton!.frame.size.height)
tRightBorder.borderWidth = width
twitterButton!.layer.addSublayer(tRightBorder)
instagramButton?.tag = 3
instagramButton!.adjustsImageWhenHighlighted = false
instagramButton?.addTarget(self, action: "tapSocial:", forControlEvents: UIControlEvents.TouchUpInside)
instagramButton?.contentMode = UIViewContentMode.Center
instagramButton?.tintColor = UIColor.whiteColor()
instagramButton?.setTitle("", forState: UIControlState.Normal)
let iBorder = CALayer()
iBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
iBorder.frame = CGRect(x: 0, y: instagramButton!.frame.size.height - width, width: instagramButton!.frame.size.width, height: instagramButton!.frame.size.height)
iBorder.borderWidth = width
instagramButton!.layer.addSublayer(iBorder)
instagramButton!.layer.masksToBounds = true
let iLeftBorder = CALayer()
iLeftBorder.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.05).CGColor
iLeftBorder.frame = CGRect(x: 0, y: 0, width: width, height: instagramButton!.frame.size.height)
iLeftBorder.borderWidth = width
instagramButton!.layer.addSublayer(iLeftBorder)
}

This came as a surprise for me but apparently there is a performance issue with textView's property "selectable" when set to true by default (checked on Storyboard).
Try going to your Storyboard, select each textView and uncheck the box "selectable". If you need the textView to be selectable, then just set selectable to true programatically on your viewDidLoad.

Related

can't push UINavigationItem on UINavigationBar in Swift4

We create navigationBar with code and set navigationItem for that navigationBar.
However, pressing navigationItem does not react.
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let navBar: UINavigationBar = {
let navBar = UINavigationBar()
navBar.frame = CGRect(x: 0, y: 44, width: view.frame.width, height: navBar.frame.height)
navBar.barTintColor = UIColor.white
let navItem = UINavigationItem()
let saveButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveBook))
navItem.rightBarButtonItem = saveButton
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel))
navItem.leftBarButtonItem = cancelButton
navItem.rightBarButtonItem?.isEnabled = true
navItem.leftBarButtonItem?.isEnabled = true
navItem.title = "書籍追加"
navBar.pushItem(navItem, animated: true)
return navBar
}()
view.addSubview(navBar)
Clearly this code is not right as the frame is still CGRect.zero. So you are setting the height to 0. You did not get what you want.
not right here:
navBar.frame = CGRect(x: 0, y: 44, width: view.frame.width, height: navBar.frame.height)
If you add the code, you will know what I mean as you cannot see the navBar?:
navBar.clipsToBounds = true
view.addSubview(navBar)
you may try:
navBar.frame = CGRect(x: 0, y: 44, width: view.frame.width, height: 44)

datePicker is not showing on desired place

i have created my custom datepicker view and added UIdatePicker and two buttons in it.
both buttons are on the right place as desired but date picker is not showing on its right place
in image Red area is datepicker
here is my code for datepicker:
#objc func datePicker(){
//DatepickerView
self.datePickerView.frame = CGRect(x: 5, y: Int(SCREEN_HEIGHT-280), width: Int(SCREEN_WIDTH-10), height: 276)
self.datePickerView.backgroundColor = UIColor.white
self.datePickerView.layer.cornerRadius = 8.0
self.datePickerView.layer.masksToBounds = true
self.datePickerView.layer.borderWidth = 2;
self.datePickerView.layer.borderColor = UIColor.black.cgColor;
doneBtn.addTarget(self, action: #selector(doneClicked), for: .touchUpInside)
doneBtn.setTitle("Done", for: .normal)
doneBtn.frame = CGRect(x: 20, y: 10, width: 80, height: 50)
doneBtn.setTitleColor(UIColor.white, for: .normal)
doneBtn.backgroundColor = UIColor.black
doneBtn.titleLabel?.font = UIFont(name: "Montserrat-Regular", size:
16.0)
doneBtn.layer.cornerRadius = 16.0
doneBtn.layer.masksToBounds = true
doneBtn.layer.borderColor = UIColor.clear.cgColor
doneBtn.layer.borderWidth = 1
cancelBtn.addTarget(self, action:#selector(cancelClicked), for: .touchUpInside)
cancelBtn.setTitle("Cancel",for: .normal)
cancelBtn.frame = CGRect(x: SCREEN_WIDTH-110, y: 10, width: 80, height: 50)
cancelBtn.setTitleColor(UIColor.white, for: .normal)
cancelBtn.backgroundColor = UIColor.black
cancelBtn.titleLabel?.font = UIFont(name: "Montserrat-Regular", size: 16.0)
cancelBtn.layer.cornerRadius = 16.0
cancelBtn.layer.masksToBounds = true
cancelBtn.layer.borderColor = UIColor.clear.cgColor
cancelBtn.layer.borderWidth = 1
// DatePicker
self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: Int(SCREEN_WIDTH-20), height: 200))
self.datePicker?.backgroundColor = UIColor.red
self.datePicker?.datePickerMode = UIDatePickerMode.date
self.datePicker?.layer.masksToBounds = true
datePicker.center = view.center
datePickerView.addSubview(datePicker)
datePickerView.addSubview(doneBtn)
datePickerView.addSubview(cancelBtn)
self.view.addSubview(datePickerView)
datePickerView.isHidden = true;
}
i have tried more than 10 answers in stackoverflow, but did not worked for me.
Any idea why it is not appearing on right place
This was an iOS 14 problem that I ran into and my datePicker was only peeking out from the bottom of the screen. I found the answer here from PetterBraka:
if #available(iOS 14, *) {
datePicker.preferredDatePickerStyle = .wheels
datePicker.sizeToFit()
}
yourTextField.inputView = datePicker
What exactly you are trying to achieve ?
According to your code :
// DatePicker
self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: Int(SCREEN_WIDTH-20), height: 200))
self.datePicker?.backgroundColor = UIColor.red
self.datePicker?.datePickerMode = UIDatePickerMode.date
self.datePicker?.layer.masksToBounds = true
datePicker.center = view.center
you are setting datePicker frame according to the view self.datePickerView and adding as subView to it. But at the same time you are adding:
datePicker.center = view.center
according to the main view which has self.datePickerView as subview.
datePicker is out of the scope in that scenario.
Also your datePicker y frame can not be 0. It will overlap done and cancel button.
Hope this will helps:
I have commented datepicker center alignment code:
// DatePicker
self.datePicker = UIDatePicker(frame:CGRect(x: Int(self.doneBtn.frame.origin.x) , y: Int(doneBtn.frame.origin.y + doneBtn.frame.size.height + 10), width: Int(cancelBtn.frame.origin.x + cancelBtn.frame.size.width - self.doneBtn.frame.origin.x), height: 200))
self.datePicker.backgroundColor = UIColor.red
self.datePicker.datePickerMode = UIDatePickerMode.date
self.datePicker.layer.masksToBounds = true
// datePicker.center = view.center

RightBarButtonItems won't be shown anymore on iOS 11

The function self.navigationItem.rightBarButtonItem = cartBarButton won't show my item on the right of UINavigationController's NavBar
EDIT
let cartOriginalImage = UIImage.cart
let cartButton = UIImageButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
cartButton.setBackgroundImage(cartOriginalImage, for: .normal)
cartButton.tintColor = Colors.Navigation.Tint.default
cartButton.addTarget(self, action: #selector(MainViewController.cartButtonPressedInHomePage(sender:)), for: .touchUpInside)
let cartBarButton = UIBarButtonItem(customView: cartButton)
if let badge = badge {
cartBarButton.badgeValue = badge
cartBarButton.badge.backgroundColor = Colors.Navigation.Background.badge
cartBarButton.badge.textColor = Colors.Navigation.Text.badge
}
I had the similar issue with custom image view. My suggestion is to wrap your custom view into container view.
private func configureUserAvatarView() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(avatarViewAction(_:)))
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
customView.addGestureRecognizer(tapRecognizer)
let imageView = FUIImageView(image: #imageLiteral(resourceName: "avatar_default_30х30"))
imageView.isCircular = true
imageView.contentMode = .scaleAspectFit
imageView.frame = customView.bounds
customView.addSubview(imageView)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: customView)
}

Put two (side by side) buttons as a right side overlay of an UITextField

(Swift3/iOS10)
I'm trying to put two buttons (save/cancel) inside of a UITextField using the rightView property. My basic setup code (called from viewDidLoad) is as follows:
private func accesorizeRenameField() {
let saveButton = UIButton(type: .system)
saveButton.setImage(#imageLiteral(resourceName: "buttonCheckmark"), for: .normal)
saveButton.addTarget(self, action: #selector(renameSave), for: .touchUpInside)
saveButton.tintColor = UIColor(white: 0.15, alpha: 1)
let cancelButton = UIButton(type: .system)
cancelButton.setImage(#imageLiteral(resourceName: "buttonX"), for: .normal)
cancelButton.addTarget(self, action: #selector(renameCancel), for: .touchUpInside)
cancelButton.tintColor = UIColor(227, 34, 60, 1)
let container = UIView()
container.addSubview(saveButton)
container.addSubview(cancelButton)
container.translatesAutoresizingMaskIntoConstraints = false
container.backgroundColor = UIColor.cyan
saveButton.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
cancelButton.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
saveButton.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
cancelButton.heightAnchor.constraint(equalTo: container.heightAnchor).isActive = true
saveButton.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
saveButton.trailingAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
cancelButton.leadingAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
cancelButton.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
self.renameField.rightView = container
self.renameField.rightViewMode = .always
}
Later on, when I expose the rename field through some animation, I call
self.renameField.rightView?.bounds = CGRect(x: 0, y: 0, width: self.renameField.bounds.height * 2, height: self.renameField.bounds.height)
self.renameField.rightView?.setNeedsLayout()
"rightView.frame \(self.renameField.rightView?.frame)".print()
self.renameField.rightView?.subviews.enumerated().forEach() {index,child in
"child \(index) frame \(child.frame)".print()
}
However, I cannot get the buttons to show up. The cyan background (for debugging) shows up, but actually is square (it should be a 2:1 rectangle). The print shows that the subviews have frames of size 0. What is the right/idiomatic way to do this? I feel like I'm just throwing hammers here...
Create UIButton as type of .custom instead of .system.
And as you already know renameField's height in viewDidLoad, you don't need to bother with constraint.
The code below is enough.
override func
viewDidLoad() {
super.viewDidLoad()
let wSize = renameField.frame.size.height - 2
let saveButton = UIButton( type: .custom )
saveButton.setImage( #imageLiteral(resourceName: "buttonCheckmark"), for: .normal )
saveButton.addTarget( self, action: #selector(renameSave), for: .touchUpInside )
saveButton.frame = CGRect( x: 0, y: 0, width: wSize, height: wSize )
let cancelButton = UIButton( type: .custom )
cancelButton.setImage( #imageLiteral(resourceName: "buttonX"), for: .normal )
cancelButton.addTarget( self, action: #selector(renameCancel), for: .touchUpInside )
cancelButton.frame = CGRect( x: wSize, y: 0, width: wSize, height: wSize )
let wV = UIView()
wV.frame = CGRect( x:0, y:0, width: wSize * 2, height: wSize )
wV.addSubview( saveButton )
wV.addSubview( cancelButton )
renameField.rightView = wV;
renameField.rightViewMode = .always;
}

Swift Change Position of UIBarButtonItem in UINavigationBar

I am doing custom a navigation bar with items which y positions are lower than default. I tried many methods:
setBackButtonBackgroundVerticalPositionAdjustment:
set CGRectOffset of custom view
UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset.init(horizontal: X, vertical: Y), forBarMetrics: UIBarMetrics.Default)
They made the items appeared in right positions, but I couldn't fire any actions when I clicked/touched on them. I think because I didn't touch the bar items, they were only the appearances/ copy views/images of these item.
Could anybody help me to resolve this problem?
My code:
let lblTitle = UILabel(frame: CGRectMake(0, 0, 100, 28))
lblTitle.text = FMLStringLocale("APP_name")
lblTitle.textColor = UIColor.whiteColor()
lblTitle.font = ROBOTO_THIN_24
lblTitle.textAlignment = NSTextAlignment.Left
lblTitle.backgroundColor = UIColor.clearColor()
lblTitle.transform = CGAffineTransformMakeTranslation(6, 11)
let lblTitleContainer = UIView(frame: lblTitle.frame)
lblTitleContainer.addSubview(lblTitle)
let leftTitle = UIBarButtonItem(customView: lblTitleContainer)
navigationItem.leftBarButtonItem = leftTitle
let rightView = UIView(frame: CGRectMake(0, 0, 134, 22))
rightView.backgroundColor = UIColor.whiteColor()
rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let imvUSFlag = UIImageView(image: UIImage(named: "icon_us"))
imvUSFlag.frame = CGRectMake(0, 0, 19, 13)
imvUSFlag.transform = CGAffineTransformMakeTranslation(-2, 20)
rightView.addSubview(imvUSFlag)
let switcher = UISwitch(frame: CGRectMake(imvUSFlag.frame.maxX - 5, 12, 31, 16))
switcher.setOn(false, animated: false)
switcher.addTarget(self, action: Selector("tapToChangeLanguage:"), forControlEvents: UIControlEvents.ValueChanged)
switcher.transform = CGAffineTransformMakeScale(0.5, 0.5)
// let switchContainer = UIView(frame: switcher.frame)
// switchContainer.addSubview(switcher)
rightView.addSubview(switcher)
let imvFRFlag = UIImageView(image: UIImage(named: "icon_france"))
imvFRFlag.frame = CGRectMake(switcher.frame.maxX + 6, 0, 19, 14)
imvFRFlag.transform = CGAffineTransformMakeTranslation(0, 20)
rightView.addSubview(imvFRFlag)
let btnMenu = UIButton()
btnMenu.setImage(UIImage(named : "icon_menu"), forState: .Normal)
btnMenu.frame = CGRectMake(imvFRFlag.frame.maxX + 30, 0, 23, 21)
btnMenu.addTarget(self, action: "tapToMenu:", forControlEvents: UIControlEvents.TouchUpInside)
btnMenu.transform = CGAffineTransformMakeTranslation(0, 18)
rightView.addSubview(btnMenu)
rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let rightViewItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightViewItem
navigationController?.navigationBar.barStyle = .BlackTranslucent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()
The reason why you can't tap your button btnMenu is that you have positioned btnMenu outside the frame of its superview rightView. A subview outside its superview cannot receive touches.
Finally, I resolved my problem:
Check my new code:
let lblTitle = UILabel(frame: CGRectMake(7, 32, 100, 28))
lblTitle.text = FMLStringLocale("APP_name")
lblTitle.textColor = UIColor.whiteColor()
lblTitle.font = ROBOTO_THIN_24
lblTitle.textAlignment = NSTextAlignment.Left
lblTitle.backgroundColor = UIColor.clearColor()
let lblTitleContainer = UIView(frame: CGRectMake(0, 0, 100, 64))
// lblTitleContainer.bounds = CGRectOffset(lblTitleContainer.bounds, -6, -32)
lblTitleContainer.addSubview(lblTitle)
let leftTitle = UIBarButtonItem(customView: lblTitleContainer)
navigationItem.leftBarButtonItem = leftTitle
let rightView = UIView(frame: CGRectMake(0, 0, 134, 64))
// rightView.backgroundColor = UIColor.grayColor()
// rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let imvUSFlag = UIImageView(image: UIImage(named: "icon_us"))
imvUSFlag.frame = CGRectMake(0, 40, 18, 12)
rightView.addSubview(imvUSFlag)
let switcher = UISwitch(frame: CGRectMake(imvUSFlag.frame.maxX - 5, 32, 31, 16))
switcher.setOn(false, animated: false)
switcher.addTarget(self, action: Selector("tapToChangeLanguage:"), forControlEvents: UIControlEvents.ValueChanged)
switcher.transform = CGAffineTransformMakeScale(0.5, 0.45)
let switchContainer = UIView(frame: switcher.frame)
switchContainer.addSubview(switcher)
rightView.addSubview(switcher)
let imvFRFlag = UIImageView(image: UIImage(named: "icon_france"))
imvFRFlag.frame = CGRectMake(switcher.frame.maxX + 6, 40, 18, 12)
rightView.addSubview(imvFRFlag)
let btnMenu = UIButton()
btnMenu.setImage(UIImage(named : "icon_menu"), forState: .Normal)
btnMenu.frame = CGRectMake(imvFRFlag.frame.maxX + 30, 35, 23, 21)
btnMenu.addTarget(self, action: "tapToMenu:", forControlEvents: UIControlEvents.TouchUpInside)
rightView.addSubview(btnMenu)
let rightViewItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightViewItem
navigationController?.navigationBar.barStyle = .BlackTranslucent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()

Resources