When I set up a button on the TabBar with Swift and pressed it, I would like to transition to the side, but the button itself is not displayed in the code below.What should I do?
let button:UIButton=UIButton()
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view.
// Create overlapping buttons on the tab bar
// I don't care about the size or location here.
let image = UIImage (named: "button.jpg")
self.button.setBackgroundImage(image, for:.normal)
self.button.addTarget(self, action:Selector(("openModal")"),
for —UIControl.Event.touchUpInside)
// AddSubview to self.tabBar instead of self.view
self.tabBar.addSubview (self.button)
}
where tabBar
is
@IBOutlet weak var tabBar:UITabBar!
Assume that it is declared and displayed correctly.
The most problematic part seems to be your own comment.
// I don't care about the size or location here.
The frame
that represents the size and location of the button
remains unconfigured in your code, but the behavior is undefined.In recent iOS, it appears to be added in size (0,0) to the upper left corner of the target view ((0,0)
).Since the size is zero, it means "not displayed."
(An older version of iOS might work differently.)
If you want to see it, set the frame
correctly.
@IBOutlet weak var tabBar:UITabBar!
let button —UIButton=UIButton()
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view.
// Create overlapping buttons on the tab bar
let image = UIImage (named: "button.jpg")
self.button.setBackgroundImage(image, for:.normal)
// ↓ Use `#selector(openModal)` instead of `Selector((("openModal")`
self.button.addTarget(self, action:#selector(openModal),
for:.touchUpInside)
// You have to be careful about the size and location here!
let origin —CGPoint=.zero// location
let size = image !.size // Size
self.button.frame = CGRect(origin:origin, size:size)
// AddSubview to self.tabBar instead of self.view
self.tabBar.addSubview (self.button)
}
@objc funcopenModal(){
print(#function)
//...
}
Although it is not related to the direct 表示not displayed されません event, writing Selector(("openModal")
is deprecated.Be sure to use the #selector
syntax.This allows the Swift compiler to check for misspelling of method names and missing @objc
when compiling.
Also, trying to change the view hierarchy or behavior of standard UI components such as UITabBar
may not work well depending on the iOS version, confusing users, and Apple may determine that they do not conform to the Human Interface Guideline.
(When I follow the reason why it was rejected from the App Store online recently, I think the HIG compliance check is loose.)
If you could show me why you're trying this, I might be able to offer you a better alternative.
© 2024 OneMinuteCode. All rights reserved.